jQuery event handler .on() not working

前端 未结 6 1244
小鲜肉
小鲜肉 2020-12-08 09:49

I want to attach a event to dynamically created element class.So i used live function but it was not triggered. So checked live function reference ,there i red below notes

6条回答
  •  南笙
    南笙 (楼主)
    2020-12-08 10:08

    I was using jQuery 1.7.2 and tried all proposed methods:

    Didn't work:

    $(document.body).on('click', '.collapsible-toggle'  function() {
        console.log('clicked');
    }); 
    

    Didn't work:

    $(document).on('click', '.collapsible-toggle'  function() {
        console.log('clicked');
    }); 
    

    None of them worked until I tried the following:

    ----- Worked! ----

    $('body .collapsible-toggle').on('click',   function() {
            console.log('clicked');
    }); 
    

提交回复
热议问题