简体   繁体   English

jQuery与动态生成的元素

[英]jQuery with elements generated dynamically

I've been working with jQuery for a pair of weeks and I've noticed it works fine with objects that are in the original HTML document, but when I generate a new element using jQuery the library doesn't get any of its events. 我已经用jQuery工作了两个星期,我注意到它与原始HTML文档中的对象一起工作正常,但是当我使用jQuery生成一个新元素时,库没有得到它的任何事件。

Let's say I try to run something like this: 假设我尝试运行这样的事情:

$('.whatever').click(function() {
  alert("ALERT!");
});

If the HTML does have something like this: 如果HTML确实有这样的东西:

<span class="whatever">Whatever</span>

Then clicking on the word Whatever gets me a nice alert. 然后点击Whatever这个词给我一个很好的警报。

But if the span element is added dynamically using jQuery, nothing happens when clicking on it. 但是如果使用jQuery动态添加span元素,则单击它时没有任何反应。

Is there a way to make jQuery work with those elements, somehow? 有没有办法让jQuery与这些元素一起工作?

Thats because the : (corrected) 那是因为:( 更正)

$('.whatever').click(function() {
  alert("ALERT!");
});   

Means, in literal terms: 用字面意思表示:

Find all elements currently on the page that have the class ".whatever"
Foreach element in that result set, bind this function to its click event

so naturally, adding a new DOM element wont automagically apply the click. 所以很自然地,添加一个新的DOM元素不会自动应用点击。

the best way to solve this is create bindings during your insert phase, ie: 解决此问题的最佳方法是在插入阶段创建绑定,即:

  var x = document.createElement("span"); 
  $(x).click(function(){ });  //etc 
  $(somcontiner).append(x); 

Warning on simply rebinding everything 简单地重新绑定一切的警告

If done wrong, it can lead to undesired effects, ie, making the number of times the event triggers something increase. 如果做错了,它可能会导致不良影响,即使事件触发事件增加的次数。 To stop this, you may need to first unbind them to delete the previous passes binds. 要停止此操作,您可能需要首先取消绑定它们以删除先前的通行证绑定。

ie,

$(x).click(foo); 
$(x).click(bar);  //foo and bar should both execute. 

so to stop this, you need 所以要阻止这一点,你需要

$(x).unbind("click");
$(x).click(foo); 

in the rebind. 重新绑定。

If you have jQuery 1.3 or later, try using live for adding events to dynamically generated elements: 如果您有jQuery 1.3或更高版本,请尝试使用live将事件添加到动态生成的元素:

$('.whatever').live("click", function() {
  alert("ALERT!");
});

Thanks everybody. 谢谢大家。

Somehow I thought elements were added to the DOM automatically by jQuery just by adding them anywhere. 不知怎的,我认为jQuery只需将它们添加到任何地方就可以自动将元素添加到DOM中。

I've also found some extra information on the topic: 我还发现了一些关于这个主题的额外信息:

http://docs.jquery.com/Frequently_Asked_Questions#Why_do_my_events_stop_working_after_an_AJAX_request.3F http://docs.jquery.com/Frequently_Asked_Questions#Why_do_my_events_stop_working_after_an_AJAX_request.3F

http://learningjquery.com/2008/03/working-with-events-part-1 http://learningjquery.com/2008/03/working-with-events-part-1

http://learningjquery.com/2008/05/working-with-events-part-2 http://learningjquery.com/2008/05/working-with-events-part-2

Just in case somebody else needs it. 以防其他人需要它。

You need to rebind it. 你需要重新绑定它。

function bindme(){
    $('.whatever').click(function(){
        alert('binded');
    });
};

bindme();

//function that will generate something

function foo(){
   $('.whatever').val('oryt');
   bindme();//rebind itagain
}

另请参阅reglib ,这是一个类似的库,允许您正在寻找的编程风格。

此外,还有一个非常出色的jQuery插件可以解决这个问题,称为livequery

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM