在两个 div 标签之间找到 p 标签
finding p tag between two div tags
我想使用 jQuery 找出两个不同 div 之间的
标签。例如
<div class="abc">
<div class="pqr">
<h2>Header Two</h2>
<p>some text</p>
</div>
</div>
<p>some text 1</p>
<div class="lmn">
<p> some text </p>
</div>
所以我想找到"some text 1"的p。 (文本可以是任何内容。)
谁能告诉我该怎么做?
这是一种方法:
基本上就是利用+
标签,判断是否存在这样的模式,然后获取需要的内容
if($('.abc + p + .lmn').length) { // + matches the elements at the same level
var x = $('.abc + p').text(); //Now that such a pattern exists , note that it could be multiple, so handle it appropriately, fetch the text
console.log(x);
}
以下将找到所有 p
标签,其前面的兄弟 .abc
和后面的兄弟 .lmn
:
$('.abc + p + .lmn').prevUntil('.abc','p')
如果您只想在两个 div
之间添加任何 p
标签,则执行
$('div + p + div').prevUntil('div','p')
你可以获取文档中的所有p标签,然后检查父标签是否为div标签,然后获取非父标签div的p标签文本,如下所示:
$(document).ready(function(){
$("p").each(function(index){
if (!$(this).parent().is("div")) alert($(this).text())
})
})
我想使用 jQuery 找出两个不同 div 之间的
标签。例如
<div class="abc">
<div class="pqr">
<h2>Header Two</h2>
<p>some text</p>
</div>
</div>
<p>some text 1</p>
<div class="lmn">
<p> some text </p>
</div>
所以我想找到"some text 1"的p。 (文本可以是任何内容。)
谁能告诉我该怎么做?
这是一种方法:
基本上就是利用+
标签,判断是否存在这样的模式,然后获取需要的内容
if($('.abc + p + .lmn').length) { // + matches the elements at the same level
var x = $('.abc + p').text(); //Now that such a pattern exists , note that it could be multiple, so handle it appropriately, fetch the text
console.log(x);
}
以下将找到所有 p
标签,其前面的兄弟 .abc
和后面的兄弟 .lmn
:
$('.abc + p + .lmn').prevUntil('.abc','p')
如果您只想在两个 div
之间添加任何 p
标签,则执行
$('div + p + div').prevUntil('div','p')
你可以获取文档中的所有p标签,然后检查父标签是否为div标签,然后获取非父标签div的p标签文本,如下所示:
$(document).ready(function(){
$("p").each(function(index){
if (!$(this).parent().is("div")) alert($(this).text())
})
})