When you inject code into the DOM, the jQuery event handlers are not attached/bound to the new elements. (jQuery already did the bindings to DOM elements before the new code was injected). Therefore, when you click a button, no jQuery click event is trapped.
To attach event handlers (and thereby grab events from) injected elements, you must use jQuery .on()
, as follows:
jsFiddle Demo
$(".statiscDIV").append('<div>FIRST</div>');
$(".statiscDIV").append('<div>hello <button class="MakeHoldDetailLinkButton">View</button> </div>');
$(document).on('click','.MakeHoldDetailLinkButton',function(){
showMakeAndHold();
});
function showMakeAndHold() {
alert("HIIIIIII");
}
The .on()
method was added in jQuery 1.7 to replace bind()
, .delegate()
, and .live()
-- it does the same thing as all of these. (To unbind event handlers from ANY DOM elements, use .off()
)
Source: http://api.jquery.com/on/
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…