我有这个结构:
<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>
Run Code Online (Sandbox Code Playgroud)
我想把文本放在div中,但只有带有这个词的文本:good.并替换.所以我这样做:
$("#thetable div:contains('good')").each(function () {
try {
console.log($(this).text());
console.log("------------------------------");
} catch (ex) {
}
})
Run Code Online (Sandbox Code Playgroud)
但是console.log告诉我,每个日志不仅包含具有良好文本的div的文本,还包括其他两个.它是印刷:
(This text is …Run Code Online (Sandbox Code Playgroud)