阅读背景:

如何在段落中找到不强的元素

来源:互联网 

I have a paragraph like so:

我有一个这样的段落:

<p>
    <strong>Cost:</strong>
    

I have a paragraph like so:

我有一个这样的段落:

<p>
    <strong>Cost:</strong>
    $1,500 - $10,000 (see below for details)
</p>

where I want to select first the strong text and then the not strong text.

我想先选择强文本,然后选择不强文本。

I was trying the following:

我正在尝试以下方法:

$('p').not('strong').html()

and

$('p:not("strong")')

but it doesn't seem to be working.

但它似乎没有起作用。

3 个解决方案

#1


If you need p elements that do not has strong in them, then you need to combine :has selector as well

如果你需要p元素不具有强大的元素,那么你需要结合:还有选择器

$('p:not(:has(strong))')

for getting the text node text without strong contents:

获取没有强内容的文本节点文本:

$('p').clone().children().remove().end().text();//returns  $1,500 - $10,000 (see below for details)

for getting the strong element text:

获取强元素文本:

 $('p strong').text();//return Cost:

Working Demo

#2


Try this:

$("p").clone().children().remove().end().text();

#3


Try

var elems = $.parseHTML($("p").html()).filter(function(elem, i) {
  return elem.tagName === "STRONG" 
         || elem.previousSibling !== null 
            && elem.previousSibling.tagName === "STRONG"
});
// `elems[0]`:`STRONG` element , `elems[1]`:`#text` node;
// `$(elems).eq(0)`:jQuery object `$("strong")`,
// `$(elems).eq(1)`:jQuery object `$("text")`
console.log(
  elems[0], elems[1],  
  $(elems).eq(0).text(), $(elems).eq(1).text()
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<p>
  <strong>Cost:</strong>
  $1,500 - $10,000 (see below for details)
</p>


,500 - ,000 (see below for details) </p> <p>



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: