JQuery .load() makes content not clickable after load

后端 未结 3 1677
北海茫月
北海茫月 2021-01-23 16:03

I have a page in which I have a div. The content from that div is being populated by an includes page, and that includes page is making a call to a database to retrieve content

相关标签:
3条回答
  • 2021-01-23 16:37

    change this line

    $(".newtask").click(function(){
    

    to

    $(document).on(".newtask","click",function(){
    

    or use delegate

    $(document).delegate("click",".newtask",function(){
    

    reason is the dynamically added elements do not get themselves attached to event handlers use on if you are using jQuery 1.7+ else use delegate

    0 讨论(0)
  • 2021-01-23 16:42

    The jQuery .click event is not a live event, so it will only make items it has been assigned to clickable. Once those items are refreshed, removed or new items come in, the refreshed items won't have the click event and new items won't have the click event.

    You'll want to use the jQuery .on event, which is the replacement for the .live event. That way when the contents refresh, they'll still have a click event attached.

    The other option is to assign the click event whenever the content refreshes.

    0 讨论(0)
  • 2021-01-23 16:59

    it will work with this

    $(document).on("click",".newtask",function(){
    

    or

    $(document).delegate(".newtask","click",function(){
    
    0 讨论(0)
提交回复
热议问题