获取每个div内的文本值取决于文本

Za7*_*7pi 4 html javascript jquery

我有这个结构:

<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 good)some text2some text3
Run Code Online (Sandbox Code Playgroud)

所以我想只使用带有文本的div,并将此文本替换为另一个文本.

谢谢!

基本上:我想得到div,我有"好"(或任何我想要的单词),并在此div插入/替换文本.在所有文档中查找,或者特别是在文档的一部分中查找,例如:在表格中.

Aru*_*hny 7

因为你有一个div包含那3个div

一种可能的解决方案是仅获取最终元素

$("#thetable div:contains('good'):not(:has(*))").each(function () {
})
Run Code Online (Sandbox Code Playgroud)

演示:小提琴