jTemplates escape {$

馋奶兔 提交于 2019-12-12 12:34:36

问题


is there a way with jTemplates to escape {$, so i can use inline javascript in my onBlur like

<a href="http://www.telegraaf.nl" onclick="if ( a ) {$('#something').css    ('display','none');alert('some msg');}">telegraaf</a>

which gets this after processTemplate:

<a onclick="if ( a ) " href="http://www.telegraaf.nl">

Thanks, Henk


回答1:


jTemplates has a {#literal} ... {#/literal} tag that should prevent your curly braces from being affected.

<a href="http://www.telegraaf.nl" onclick="{#literal}if ( a ) {$('#something').css    ('display','none');alert('some msg');}{#/literal}">telegraaf</a>



回答2:


Actually, in my opinion, I think its best to attach the event unobtrusively :

$(function () {
    $(".alink").click(function () {
        //if ( a ) {
            $('#something').css('display','none');
            alert('some msg');
        //}   
    });
});

<a class="alink" href="http://www.telegraaf.nl">



回答3:


If you're using jQuery then the $ is essentially just a shortcut to saying jQuery(expression) so in your case you can use:

<a href="http://www.telegraaf.nl" onclick="if ( a ) {jQuery('#something').css    ('display','none');alert('some msg');}">telegraaf</a>

You can read up on the selector shortcut at http://docs.jquery.com/%24




回答4:


If you don't want to move your JS to separate secion or external file then you can always use jQuery "keyword" instead of $

<a href="http://www.telegraaf.nl" onclick="if( a ) {jQuery('#something').css('display','none');alert('some msg');}">telegraaf</a>

This way $ won't be interpreted as a template variable.




回答5:


var test = function(el) {
   if ( a ) {
      $('#something').css('display','none');
      alert('some msg');
    }   
});

<a onclick="test(this);" href="http://www.telegraaf.nl">


来源:https://stackoverflow.com/questions/1238449/jtemplates-escape

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!