英文:
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 -->
<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>
<!-- end snippet -->
Selecting the b tags is easey:
$('.boxMain b').toArray().map(item => $(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("b").nextSibling.textContent);
<!-- language: lang-html -->
<h1>test</h1>
<p>this is a test</p>
<b>bold</b>
text here
<span>span element</span>
<!-- 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("trust", "us")"><span class="ico icoAuUs"></span></i><i id="phSep"></i>/trʌst/<i class="aud" onclick="tts("trust", "uk")"><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("trust", "us")"><span class="ico icoAuUs"></span></i><i id="phSep"></i>/trʌst/<i class="aud" onclick="tts("trust", "uk")"><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 = $($('.boxMain b').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't want ones that are just line breaks or whitespace
var textNodes = parentHtmlCopy
.toArray()
.filter((el) => {
//console.log(el.tagName, el.textContent)
return el.tagName === undefined && el.textContent.trim() !== '';
});
//Only keep the actual text content in our array instead of the text node object
var justText = textNodes.map((el) => {
//console.log(el.textContent)
return el.textContent.replace(':', '').trim();
});
console.log(justText)
<!-- language: lang-html -->
<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>
<!-- 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 -->
$('.boxMain b').each((i, el)=>{
var key = el.textContent.trim();
var val = el.nextSibling.textContent.replace(':','').trim()
console.log(key, ' -- ', val)
});
<!-- language: lang-html -->
<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>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论