Javascript 在jQuery中选择包装元素
我正在使用jquery和nodejs在一个非常旧的web中进行一些过滤,我想选择由aJavascript 在jQuery中选择包装元素,javascript,jquery,css,node.js,Javascript,Jquery,Css,Node.js,我正在使用jquery和nodejs在一个非常旧的web中进行一些过滤,我想选择由ap包装的b元素。我的意思是只有那些用p包装而没有文本的 我想要匹配的内容 <p><b>Some text</b></p> <p>Some other text and some other elements <a>link</a><b>Some text</b> and some more tex
p
包装的b
元素。我的意思是只有那些用p
包装而没有文本的
我想要匹配的内容
<p><b>Some text</b></p>
<p>Some other text and some other elements <a>link</a><b>Some text</b>
and some more text of the parent p
</p>
一些文本
我不想匹配的内容
<p><b>Some text</b></p>
<p>Some other text and some other elements <a>link</a><b>Some text</b>
and some more text of the parent p
</p>
一些其他文本和一些其他元素链接一些文本
还有一些父级p的文本
你知道我可以选择第一个包装的元素而不是第二个吗?是否有任何选择器可以选择此选项?您可以使用
过滤器
方法:
$('p > b').filter(function() {
return $(this.parentNode) // get the p element
.contents() // get all the child nodes
.not(this) // exclude the current `b` element
.length === 0;
});
尝试使用
:第一个孩子
选择器
alert($(“>b:第一个孩子”,“p”).text())
一些文本
我不想匹配的东西
一些其他文本和一些其他元素链接一些文本
还有一些父级p的文本
不确定。你看起来像这样吗
HTML
<p>Some other text and some other elements <a>link</a><b>Some text</b>
and some more text of the parent p</p>
<p><b>Some text</b></p>
<p>Some other text and some other elements <a>link</a><b>Some text</b>
使用substring()
检查html()
的前3个字母是否等于
$(“p”)。每个(函数(){
if($(this.html().substring(0,3)==“”){
//做任何事
}
});
如图所示。
$('p b:empty')
试试这个检查这个小提琴谢谢@guradio,但这不是我想要的,我想我不需要把它作为一个答案,因为你已经接受了一个已经很快乐的编码是的!这就是我想要的如果你想要的元素不是第二个。。。?