0
$('.collapse').each(function() {
  var title=  $(this).siblings('.accordion-heading').find('a');
  $(this).on('show hide', function (e) {
    if(!$(this).is(e.target))return;
    title.parent().toggleClass('active', 300);
    title.parent().hasClass('active') ? $('input.party').prop('value', '') : $('input.party').val(title.siblings('.delete').prop('id'));

    var id = title.siblings('.delete').prop('id');
    var data = {id: id};
    $.post("times.php", data, function(result) {
      if(title.parent().hasClass('active')){
        $('.times').html('');
      } else {
        $('.times').html($.parseJSON(result));
      }
    })
  })
}) 

因此,我通过添加一个新派对在我的 html 中添加了一个新的手风琴组,并且我不希望所有这些都适用于新添加的元素。我没有找到可以帮助我的主题,因为它比任何随机each函数都更具体(我认为)。这个未来元素的东西对我来说是新的,所以我会很感激一些解释或一个很好的链接到我已经检查过的 jquery 网站以外的地方。感谢您的时间!

基本上我想$(this).on('show hide', function (e) {用类似$(document).on('show hide', $(this), function (e) {. 我刚刚写的东西不起作用。

4

2 回答 2

1

如果它只是关于事件处理程序,那么您也可以使用事件委托来捕获动态创建的元素上的事件。

没有理由你必须在.each这里使用,所以省略它:

$(document.body).on('show hide', '.collapse', function() {
    var title =  $(this).siblings('.accordion-heading').find('a');
    if(!$(this).is(e.target))return;
    // rest of the code...
});
于 2013-06-13T14:55:48.897 回答
1

这将适用于匹配选择器的任何新对象

jQuery(document).on('show hide', '.accordion-heading a', function(event){
    ...
});
于 2013-06-13T14:17:26.387 回答