gpt4 book ai didi

javascript - 我怎样才能使用 jQuery 在带有子元素的元素中获取文本

转载 作者:行者123 更新时间:2023-11-30 10:56:25 25 4
gpt4 key购买 nike

我将文本存储在一个包含多个 span 标签的变量中。
我想一次获取每个跨度内的所有内容。我如何使用 jQuery 或 javascript 做到这一点?

<htmlText>
Researchthe university has been given a 5 star rating in the current Research Assessment Exercise. It is one of the UK's leading technological universities, specialising in research to provide solutions critical to industry, commerce and the healthcare, voluntary and public sectors.
<span class="NBResult"></span><span class="NBResult">Data transmission speed</span>
<span>, green fuels,small business marketing needs, European bureaucracy and the treatment </span>
<span class="NBSearchTerm">of</span>
<span> </span><span class="NBSearchTerm">cancer</span>
<span> are all under investigation at the university. </span>
</htmlText>

最佳答案

更新:此解决方案已根据评论中的其他问题进行了更新:

只需获取父选择器的 .text() 属性即可。

var myWords = "<span>I</span><span> </span><span>like</span><span> </span><span>words.</span>";

/* "I like words" */
alert($(myWords).find("span").text());
alert($("p.words span").text());

<p class="words">
<span>I</span><span> </span><span>like</span><span> </span><span>words.</span>
</p>

关于javascript - 我怎样才能使用 jQuery 在带有子元素的元素中获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1043310/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com