我需要选择第一部分的帮助。你好,我叫乔。然后我需要隐藏另一部分。
<p>
<b>NAME</b>
<br>
<br>
"hello my name is joe"
<br>
<br>
<b>AGE</b>
<br>
<br>
"My age is 22"
<br>
</p>
答案 0 :(得分:0)
在拥有文本节点的情况下,您需要在p
上使用.contents()
,然后才能遍历并删除第二个b
标签上或之后的任何节点
var counter = 0;
var contents = $('p').contents().each(function() { // loop through all nodes in the p
var $this = $(this);
if ($this.is('b')) { // count the b tags
counter++;
}
if (counter >= 2) { // remove anything on or after the second b
$this.remove();
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>
<b>NAME</b>
<br>
<br> "hello my name is joe"
<br>
<br>
<b>AGE</b>
<br>
<br> "My age is 22"
<br>
</p>