在jQuery中的document.ready中分配的click() [英] click() assigned in document.ready in jQuery
问题描述
document.ready( click(fn))中的分配是否适用于与选择器匹配的新附加元素?
Do assignments in document.ready (click(fn) specifically) apply to newly appended elements that match the selector?
如果不是,我该如何将它分配给这个新元素?我是否必须在每次追加后编写作业或是否有更好的方法?
If not, how can I assign it to this new elements? Do I have to write the assignment after every append or is there a better way?
推荐答案
您正在寻找实时功能。根据手册:
You are looking for the live functionality. Per the manual:
将处理程序绑定到所有当前和未来匹配元素的事件(如单击)。也可以绑定自定义事件。
Binds a handler to an event (like click) for all current - and future - matched element. Can also bind custom events.
所以如果你这样做:
$(document).ready(function() {
$('div.test').live('click', function() { alert('yipee!'); });
$('body').append('<div class="test">Click me!</div>');
});
当您点击div时,即使在事件被绑定后添加了警报,您也会收到警报。
When you click on the div you will get the alert even though it was added after the event was bound.
这篇关于在jQuery中的document.ready中分配的click()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!