简体   繁体   English

为什么 jQuery 不能处理附加内容?

[英]Why is jQuery not working on appended content?

I have a jQuery function that displays a hidden content on each element in the loop of elements.我有一个 jQuery 函数,它在元素循环中的每个元素上显示隐藏的内容。 This work perfectly, but the problem is that when I append new elements to the loop it stops working.这工作完美,但问题是当我将新元素附加到循环时,它停止工作。 It actually works on some elements and doesn't work on others (strange behavior).它实际上适用于某些元素而不适用于其他元素(奇怪的行为)。

<script type="text/javascript">
 jQuery(document).ready(function($) {

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

 if ($(this).next().css('display') == 'none'){
 $(this).next().show();

}
else

if($(this).next().is(':visible')){
   $(this).next().hide();
 }

});
})
</script>

Is there a problem with my jQuery code?我的 jQuery 代码有问题吗? How can I make it work on both newly appended elements and previously displayed elements.我怎样才能让它在新添加的元素和以前显示的元素上都能工作。

HTML code HTML代码

   <div class="center_container">
   <%for users in @user%>
  <div class="see_all">

  <span style="color:#808080;cursor:pointer">See All Reviews(<%=@beep_count=       (@beep.opposes.count +  @beep.neutrals.count + @beep.supports.count)%>)</span>

 </div>

     <div style="display:none;" class="hidden">

      No reviews available 

     </div>
 <%end%>

 </div>

This will only work if you're using jQ 1.7+ .这仅在您使用 jQ 1.7+ 时才有效 If you're not, take a look at jQuery's delegate method , which will achieve the same thing in jQuery < 1.7.如果不是,请查看 jQuery 的委托方法,它将在 jQuery < 1.7 中实现相同的功能。 There's no reason not to be using the latest version, though :)不过,没有理由不使用最新版本:)

This is because at the time when you bind your click handler:这是因为在绑定点击处理程序时:

 $(".see_all").click()

It is bound only to the current elements that match the selector .see_all .它必然以匹配选择当前元素.see_all

What you can do to get around this is use jQuery's on() to delegate:您可以做的是使用 jQuery 的on()来委托:

$("body").on("click", ".see_all", function() {
  //do stuff
});

This handler is bound to the body , and then when it detects a click it will look to see if the element clicked matches the selector .see_all .此处理程序绑定到body ,然后当它检测到单击时,它将查看单击的元素是否与选择器.see_all匹配。 If it does, it will then execute the function.如果是,它将执行该函数。 Because it's bound to the body , this will work if new .see_all elementsare inserted into the DOM too.因为它绑定到body ,如果新的.see_all元素也插入到 DOM 中,这将起作用。

If all .see_all are contained within one div , use that to delegate on rather than body - used that purely as an example.如果所有.see_all都包含在一个div ,请使用它来委托 on 而不是body - .see_all作示例。 Delegate to the first thing that encompasses all .see_all elements.委托给包含所有.see_all元素的第一件事。

As a side note, you should use the new API .on() and .off() for your event binding, since 1.7 they have become the preferred method.作为旁注,您应该使用新的 API .on().on() .off()进行事件绑定,自 1.7 以来,它们已成为首选方法。 Documentation for .on() . .on() 的文档

First of all, this can be optimized using toggle.首先,这可以使用切换进行优化。 Then, you will need an on() event handler for this, since click is only bound to already existing elements on document ready.然后,您将需要一个 on() 事件处理程序,因为 click 仅绑定到文档就绪时已经存在的元素。 When a new element is added, you need a way to dynamically re-bind the click event.当添加新元素时,您需要一种动态重新绑定点击事件的方法。

<script type="text/javascript">
 jQuery(document).ready(function($) {

 $("body").on('click', '.see_all', function(){
  $(this).next().toggle();
});
})
</script>

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

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