gpt4 book ai didi

javascript - 根据文本获取每个 div 内的文本值

转载 作者:搜寻专家 更新时间:2023-11-01 05:00:56 26 4
gpt4 key购买 nike

我有这样的结构:

<table id="thetable">
<tr>
<td>
<div>
<div>some text</div>
<div>
<div></div>
<div>(This text is good)</div>
<div>some text2</div>
<div>some text3</div>
</div>
</div>
</td>
<tr>
</tr>
<td>
<div>
<div>some text</div>
<div>
<div></div>
<div>(This text is good)</div>
<div>some text2</div>
<div>some text3</div>
</div>
</div>
</td>
</tr>
<tr>
<td>
<div>
<div>some text</div>
<div>
<div></div>
<div>(This text is good)</div>
<div>some text2</div>
<div>some text3</div>
</div>
</div>
</td>
</tr>
</table>

我想获取 div 中的文本,但只有带有这个词的文本:good。并更换。所以我这样做:

$("#thetable div:contains('good')").each(function () {
try {
console.log($(this).text());
console.log("------------------------------");
} catch (ex) {

}
})

但是 console.log 对我说,每个日志不仅包含具有良好文本的 div 的文本,还包含其他两个。正在打印:

(This text is good)some text2some text3

所以我只想获取文本良好的 div 并将此文本替换为另一个文本。

谢谢!

基本上:我想获得包含“good”(或任何我想要的词)一词的 div,并在此 div 中插入/替换文本。在所有文档中查找,或者更具体地说,在文档的一部分中查找,例如此示例:在表格中。

最佳答案

因为你有一个包含这 3 个 div 的 div

一个可能的解决方案是像这样只获取结束元素

$("#thetable div:contains('good'):not(:has(*))").each(function () {
})

演示:Fiddle

关于javascript - 根据文本获取每个 div 内的文本值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22959422/

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