jquery 过滤有 +不是
好的,我有列表项,有些有跨度,有些没有。
在我的活动中,当他们还没有跨度时,我想添加跨度。
has()
工作正常,但 not()
将跨度添加到两者?
HTML:
<ul>
<li>
<p>item</p>
<span class="spn">empty span</span>
</li>
<li>
<p>item 2</p>
</li>
<ul>
<hr>
<a class="add-span"href="#">check</a>
JS:
$("a.add-span").click(function() {
$("li").each(function(index) {
// $(this).has("span").find("span").append(" - appended");
$(this).not("span").append('<span class="spn">new span<\/span>');
})
})
Okay I have list items, some have a span, some not.
On my event I want to add the span when they don't have any yet.
has()
works fine, but not()
adds the span to both??
HTML:
<ul>
<li>
<p>item</p>
<span class="spn">empty span</span>
</li>
<li>
<p>item 2</p>
</li>
<ul>
<hr>
<a class="add-span"href="#">check</a>
JS:
$("a.add-span").click(function() {
$("li").each(function(index) {
// $(this).has("span").find("span").append(" - appended");
$(this).not("span").append('<span class="spn">new span<\/span>');
})
})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以结合使用 :not 和 :has 像这样的选择器
这是一个 演示 http://www.jsfiddle.net/xU6fV/
You can use a combination of the :not and :has selectors like this
Here is a demo http://www.jsfiddle.net/xU6fV/
对于
children()
方法,length
属性用于检查span
是否已存在,如果不存在,则为添加/附加。更多信息:
With
children()
method, thelength
property is used to check whether or not aspan
already exits and if it doesn't, one is added/appended.More Info:
如果您不想仅过滤掉那些将
span
作为直接子元素的元素,则接受的答案效果很好。由于
:has()
和.has()
循环遍历所有后代,而不仅仅是子代。在这种情况下,你必须使用一个函数
The accepted answer works well, if you don't want to filter out only those elements which do have the
span
s as direct children.As the
:has()
and the.has()
loop over all descendants, not just the children.In that case, you have to use a function
这是搜索“jquery not has”时 Google 上的第一个链接。我需要解决的具体场景与此略有不同。我必须
not
具有特定 DOM 元素的项目,而不仅仅是基于标签。这意味着我无法使用上面每个解决方案使用的选择器。然而,jQuery 的
has()
确实接受 DOM 元素!所以我创建了一个 jQuery 插件来组合这两个内置函数。我想在这里分享它,因为希望它也能帮助其他处于我这种情况的人。这也回答了原来的问题。
插件:
实现:
https://jsfiddle.net/brjkg10n/1/
我最初打算找到一个像
addBack()
但使用的 jQuery 函数不是
。如果您需要如此复杂的东西,请随意使用以下插件。有了这个,答案将是:
https://jsfiddle.net/2g08gjj8/1/
This is the first link on Google when searching "jquery not has". The specific scenario I needed to solve was a little different to this one. I had to
not
items that have a specific DOM element, not simply based on a tag. This meant I couldn't use a selector which each solution above used.jQuery's
has()
however does accept a DOM element! So I created a jQuery plugin to combine these two inbuilt functions.I wanted to share it here because hopefully it will help others in my situation too. It also answers the original question.
The Plugin:
Implementation:
https://jsfiddle.net/brjkg10n/1/
I was initially intending to find a jQuery function that would work like
addBack()
but usingnot
instead. If you need something so complicated, then please feel free to use the following plugin.With this, the answer would be:
https://jsfiddle.net/2g08gjj8/1/
试试这个,
Try this,