0

生成的输出如下: HTML

<div id="resultCancel" class="error" style="display: block;">
    <a class="close">X</a>
    <ul style="">
        <li for="ProductId" generated="true" class="error" style="">Please Enter License Key</li>
    </ul>
</div>

当您单击 a.close 时,我需要清空 li 的内容(在有人杀死我之前,我们使用的是旧代码库,这就是我使用 .live 功能的原因)

$('a.close').live('click',function(){
   $(this).parent().fadeOut("fast");
   $(this).closest//.next//.sibling('ul').addClass('foo');
});

通过使用最接近的,我可以向上 DOM 树,我需要向下。.next 和 .sibling 没有效果....如果重要的话 div#resultCancel 是由 jQuery 验证插件触发的。

4

1 回答 1

2

您可以使用.next()

$this.next('ul').find('li.error').addClass('foo').html('Text Changed!!')

使用.on()而不是.live()后者已被弃用..

$('a.close').on('click',function(){
   var $this = $(this);
   //$this.parent().fadeOut("fast");
   $this.next('ul').find('li.error').addClass('foo').html('Text Changed!!')
});

你也需要not change the text原样hiding the parent div

检查小提琴

对于动态元素委托事件

$('body').on('click','a.close',function(){  // jQuery > 1.7.0

$('body').delegate('a.close' ,'click',function(){  // jQuery < 1.7.0
于 2012-12-12T21:41:11.803 回答