handling jQuery onClick event on handlebars

前端 未结 3 1902
花落未央
花落未央 2020-12-31 06:18

I would like to set up a simple jQuery onClick event to make the UI dynamic on a handlebars template. I was wondering to addClass() after a specific click.

consider

相关标签:
3条回答
  • 2020-12-31 07:01

    there is no need to reattach the event handler on every dynamic DOM update if you're defining them at document level:

    $(document).on('click','li',function(){
       alert( 'success' );
    });
    

    Hope this helps! :-)

    0 讨论(0)
  • 2020-12-31 07:07

    You have to refresh your Jquery listeners AFTER the insertion of nodes into your DOM HTML.

    var source   = "<li><a href="{{uri}}">{{label}}</a></li>";
    var template = Handlebars.compile(source);
    var context = {"uri":"http://example.com", "label":"my label"};
    $("ul").append( template(context) );
    
    // add your JQuery event listeners
    $("li").click(function(){ alert("success"); });
    
    0 讨论(0)
  • 2020-12-31 07:13

    I am not sure what your problem exactly is. It's correct like this if you keep your JavaScript in a *.js file, perhaps using parent() instead on prev() in this specific case.

    0 讨论(0)
提交回复
热议问题