jquery button not respond to click method

為{幸葍}努か 提交于 2019-12-04 10:33:06

Anything you add to the DOM after the document.ready has fired needs to use .live or .delegate in order to add an event handler to the newly added element.

For instance:

$('.tryAgain').live("click", function() {...});

If you are using jquery 1.7+ you should use .on:

$(document).on("click", ".tryAgain", function(){ ... });

Try with $.live o $.delegate.

Use:

$('.tryAgain').live('click', function()
{
  ....
});

There is an alternative to use live events, you can add the handler when you create the element like this:

$("<p>Try Again</p>", {
  "class": "tryAgain",
  click: function(){
     //YOUR CLICK HANDLER
  }
}).appendTo($(this).parent().parent().children("div"));

I think you can attach a click event right there when you create the new p tag like this:

$(this).parent().parent().children("div").append(
    $('<p>').addClass('tryAgain').click(function(){
        alert('test');
    }));
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!