选择位于其下的b标签的兄弟元素。

huangapple go评论71阅读模式
英文:

select sibling of b tags just under them

问题

Using Cheerio, you can select the text under the <b> tags as follows:

$('.boxMain b').nextUntil('hr').text().trim();

This code will select the text under the <b> tags and return the desired text.

英文:

Using I Cheerio I want to select the text of theses parts:

this is the first area to select

this is the second area to select

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;div class=&quot;lun boxBd boxMain&quot; t=&quot;page&quot;&gt;
  &lt;div id=&quot;boxWrd&quot; class=&quot;boxLtr&quot;&gt;
    &lt;div&gt;
      &lt;h1&gt;trust&lt;/h1&gt;&lt;i class=&quot;aud&quot;&gt;&lt;span class=&quot;ico icoAu&quot;&gt;&lt;/span&gt;&lt;/i&gt;
    &lt;/div&gt;
    &lt;div&gt;
      &lt;div id=&quot;pho&quot;&gt;/ˈtrəst/&lt;i class=&quot;aud&quot; onclick=&quot;tts(&amp;quot;trust&amp;quot;, &amp;quot;us&amp;quot;)&quot;&gt;&lt;span class=&quot;ico icoAuUs&quot;&gt;&lt;/span&gt;&lt;/i&gt;&lt;i id=&quot;phSep&quot;&gt;&lt;/i&gt;/trʌst/&lt;i class=&quot;aud&quot; onclick=&quot;tts(&amp;quot;trust&amp;quot;, &amp;quot;uk&amp;quot;)&quot;&gt;&lt;span class=&quot;ico icoAuUk&quot;&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;hr&gt;

  &lt;b&gt;meaning&lt;/b&gt;:
  this is the first area to select

  &lt;hr&gt;

  &lt;b&gt;other meanings&lt;/b&gt;:
  this is the second area to select


  &lt;div id=&quot;meanTools&quot;&gt;&lt;i class=&quot;ico icoSt&quot; onclick=&quot;starWm()&quot;&gt;&lt;/i&gt;&lt;i class=&quot;ico icoCo&quot; onclick=&quot;copyWm()&quot;&gt;&lt;/i&gt;
    &lt;cgr id=&quot;msgTools&quot;&gt;&lt;/cgr&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

Selecting the b tags is easey:

$(&#39;.boxMain b&#39;).toArray().map(item =&gt; $(item).text());

But How can we select under them and get the desired text?

答案1

得分: 2

.nextSibling

console.log(document.querySelector("b").nextSibling.textContent);
<h1>test</h1>
<p>this is a test</p>
<b>bold</b>
text here
<span>span element</span>
英文:

.nextSibling

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

console.log(document.querySelector(&quot;b&quot;).nextSibling.textContent);

<!-- language: lang-html -->

&lt;h1&gt;test&lt;/h1&gt;
&lt;p&gt;this is a test&lt;/p&gt;
&lt;b&gt;bold&lt;/b&gt;
text here
&lt;span&gt;span element&lt;/span&gt;

<!-- end snippet -->

答案2

得分: 1

// 将父元素的HTML作为字符串获取,然后使用jQuery解析以获取可选择的元素
var parentHtmlCopy = $($('.boxMain b').parent().html())

// 现在我们有了所有子元素的列表
// 我们将列表过滤,只保留我们想要的部分
// 未定义的标签只是文本节点,我们不想要只是换行符或空格的部分
var textNodes = parentHtmlCopy
  .toArray()
  .filter((el) => {
    return el.tagName === undefined && el.textContent.trim() !== '';
  });

// 只保留数组中实际的文本内容,而不是文本节点对象
var justText = textNodes.map((el) => {
    return el.textContent.replace(':', '').trim();
  });

 console.log(justText)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="lun boxBd boxMain" t="page">
  <div id="boxWrd" class="boxLtr">
    <div>
      <h1>trust</h1><i class="aud"><span class="ico icoAu"></span></i>
    </div>
    <div>
      <div id="pho">/ˈtrəst/<i class="aud" onclick="tts(&quot;trust&quot;, &quot;us&quot;)"><span class="ico icoAuUs"></span></i><i id="phSep"></i>/trʌst/<i class="aud" onclick="tts(&quot;trust&quot;, &quot;uk&quot;)"><span class="ico icoAuUk"></span></i></div>
    </div>
  </div>
  <hr>
  <b>meaning</b>:
  this is the first area to select
  <hr>
  <b>other meanings</b>:
  this is the second area to select
  <div id="meanTools"><i class="ico icoSt" onclick="starWm()"></i><i class="ico icoCo" onclick="copyWm()"></i><cgr id="msgTools"></cgr></div>
</div>
$('.boxMain b').each((i, el)=>{
  var key = el.textContent.trim();
  var val = el.nextSibling.textContent.replace(':', '').trim()
  console.log(key, ' -- ', val)
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="lun boxBd boxMain" t="page">
  <div id="boxWrd" class="boxLtr">
    <div>
      <h1>trust</h1><i class="aud"><span class="ico icoAu"></span></i>
    </div>
    <div>
      <div id="pho">/ˈtrəst/<i class="aud" onclick="tts(&quot;trust&quot;, &quot;us&quot;)"><span class="ico icoAuUs"></span></i><i id="phSep"></i>/trʌst/<i class="aud" onclick="tts(&quot;trust&quot;, &quot;uk&quot;)"><span class="ico icoAuUk"></span></i></div>
    </div>
  </div>
  <hr>
  <b>meaning</b>:
  this is the first area to select
  <hr>
  <b>other meanings</b>:
  this is the second area to select
  <div id="meanTools"><i class="ico icoSt" onclick="starWm()"></i><i class="ico icoCo" onclick="copyWm()"></i><cgr id="msgTools"></cgr></div>
</div>
英文:

This is a bit complicated, especially compared to the other answer here, but it works. The explanation is in the comments

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

//Get the HTML of the parent element as a string, then parse it with jQuery to get selectable elements
var parentHtmlCopy = $($(&#39;.boxMain b&#39;).parent().html())

//Now we have a list of all the child elements
//we filter the list down to only keep the ones we want
//undefined tags are just text nodes, and we don&#39;t want ones that are just line breaks or whitespace
var textNodes = parentHtmlCopy
  .toArray()
  .filter((el) =&gt; {
    //console.log(el.tagName, el.textContent)
    return el.tagName === undefined &amp;&amp; el.textContent.trim() !== &#39;&#39;;
  });

//Only keep the actual text content in our array instead of the text node object
var justText = textNodes.map((el) =&gt; {
    //console.log(el.textContent)
    return el.textContent.replace(&#39;:&#39;, &#39;&#39;).trim();
  });
  
 console.log(justText)

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;div class=&quot;lun boxBd boxMain&quot; t=&quot;page&quot;&gt;
  &lt;div id=&quot;boxWrd&quot; class=&quot;boxLtr&quot;&gt;
    &lt;div&gt;
      &lt;h1&gt;trust&lt;/h1&gt;&lt;i class=&quot;aud&quot;&gt;&lt;span class=&quot;ico icoAu&quot;&gt;&lt;/span&gt;&lt;/i&gt;
    &lt;/div&gt;
    &lt;div&gt;
      &lt;div id=&quot;pho&quot;&gt;/ˈtrəst/&lt;i class=&quot;aud&quot; onclick=&quot;tts(&amp;quot;trust&amp;quot;, &amp;quot;us&amp;quot;)&quot;&gt;&lt;span class=&quot;ico icoAuUs&quot;&gt;&lt;/span&gt;&lt;/i&gt;&lt;i id=&quot;phSep&quot;&gt;&lt;/i&gt;/trʌst/&lt;i class=&quot;aud&quot; onclick=&quot;tts(&amp;quot;trust&amp;quot;, &amp;quot;uk&amp;quot;)&quot;&gt;&lt;span class=&quot;ico icoAuUk&quot;&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;hr&gt;

  &lt;b&gt;meaning&lt;/b&gt;:
  this is the first area to select

  &lt;hr&gt;

  &lt;b&gt;other meanings&lt;/b&gt;:
  this is the second area to select


  &lt;div id=&quot;meanTools&quot;&gt;&lt;i class=&quot;ico icoSt&quot; onclick=&quot;starWm()&quot;&gt;&lt;/i&gt;&lt;i class=&quot;ico icoCo&quot; onclick=&quot;copyWm()&quot;&gt;&lt;/i&gt;
    &lt;cgr id=&quot;msgTools&quot;&gt;&lt;/cgr&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->


Greatly simplified version thanks to the above answer from Ronnie Royston. This is probably a much better way to go!

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(&#39;.boxMain b&#39;).each((i, el)=&gt;{
  var key = el.textContent.trim();
  var val = el.nextSibling.textContent.replace(&#39;:&#39;,&#39;&#39;).trim()
  console.log(key, &#39; -- &#39;, val)
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;div class=&quot;lun boxBd boxMain&quot; t=&quot;page&quot;&gt;
  &lt;div id=&quot;boxWrd&quot; class=&quot;boxLtr&quot;&gt;
    &lt;div&gt;
      &lt;h1&gt;trust&lt;/h1&gt;&lt;i class=&quot;aud&quot;&gt;&lt;span class=&quot;ico icoAu&quot;&gt;&lt;/span&gt;&lt;/i&gt;
    &lt;/div&gt;
    &lt;div&gt;
      &lt;div id=&quot;pho&quot;&gt;/ˈtrəst/&lt;i class=&quot;aud&quot; onclick=&quot;tts(&amp;quot;trust&amp;quot;, &amp;quot;us&amp;quot;)&quot;&gt;&lt;span class=&quot;ico icoAuUs&quot;&gt;&lt;/span&gt;&lt;/i&gt;&lt;i id=&quot;phSep&quot;&gt;&lt;/i&gt;/trʌst/&lt;i class=&quot;aud&quot; onclick=&quot;tts(&amp;quot;trust&amp;quot;, &amp;quot;uk&amp;quot;)&quot;&gt;&lt;span class=&quot;ico icoAuUk&quot;&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;hr&gt;

  &lt;b&gt;meaning&lt;/b&gt;:
  this is the first area to select

  &lt;hr&gt;

  &lt;b&gt;other meanings&lt;/b&gt;:
  this is the second area to select


  &lt;div id=&quot;meanTools&quot;&gt;&lt;i class=&quot;ico icoSt&quot; onclick=&quot;starWm()&quot;&gt;&lt;/i&gt;&lt;i class=&quot;ico icoCo&quot; onclick=&quot;copyWm()&quot;&gt;&lt;/i&gt;
    &lt;cgr id=&quot;msgTools&quot;&gt;&lt;/cgr&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月20日 22:43:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76295782.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定