JQuery 选择器:href 属性既不包含 word1 也不包含 word2
我是 JQuery 新手,我认为这非常简单,但我找不到正确的选择器。我尝试使用这段代码:
$(":not(a[href=word1],a[href=word2]").click(function() {
... do something
}
但它不起作用,事实上它似乎耗尽了所有的CPU时间。你能帮助我吗?谢谢!
I'm new to JQuery, i suppose that's really simple but I can't find the right selector. I tried with this code:
$(":not(a[href=word1],a[href=word2]").click(function() {
... do something
}
but it doesn't work, in fact it seems to suck all the cpu time. Can you help me? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个更有效的版本,首先将范围缩小到锚点:
您可以在此处测试选择器。
目前,您正在尝试将
click
事件绑定到所有,该事件不是其href
属性中包含这些单词之一的锚点...every元素的检查 和 绑定都非常昂贵,相反,只有当它们的href
不包含这些单词中的任何一个时,它才会绑定到锚点。Here's a much more efficient version that narrows it down to anchors first:
You can test the selector out here.
Currently you're trying to bind a
click
event to everything that's not an anchor with one of those words in it'shref
attribute...every element which is very expensive both to check and to bind, this instead binds to anchors and only then if theirhref
doesn't contains either of those words.