jquery .not()与live不兼容()
问题描述:
我在页面中添加了一些dom与ajax,我试图通过.not()
排除选择的结果,但它不适用于我。jquery .not()与live不兼容()
下面是一个简单的例子:
HTML:
<div class="list-item">
<span class="item-title">Some title</span>
<span class="edit fr"><a href="Edit link">Edit</a></span>
<span class="trash fr"><a href="#trashit" rel="trash">Trash</a></span>
<span class="live fr active"><a href="#liveit" rel="publish">Live</a></span>
<span class="draft fr"><a href="#draftit" rel="draft">Draft</a></span>
</div>
的jQuery:
jQuery('.list-item span a').not('.list-item span.edit a').live('click', function(){
//do stuff
});
观察:
- 如果我删除代码的
.not()
部分,则选择与live()
一起工作正常。 - 如果我使用
.not()
,但用click()
代替live()
代码有效。
但我需要与live();
(阿贾克斯包括DOM)
提前感谢运行.not()
(排除.edit
类)!
答
DOM遍历方法不支持寻找元素,以发送到
.live()
。相反,应该总是在选择器之后直接调用.live()
方法,如上例所示。
所以,该类排除加入选择:
jQuery('.list-item span:not(.edit) a').live('click', function(){
//do stuff
});
答
尝试
$('.list-item span:not(.edit) a').live(...);
正如别人指出的,DOM遍历不支持直播。 live也有其他一些限制,请检查文档。
答
jQuery('.list-item').filter(function(){
return $(this).not("span.edit a")
})
.live('click', function(){
//do stuff
});
感谢您指出这一点。像魅力一样工作! – Sisir