小编Sta*_*984的帖子

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

我有这个结构:

<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)

html javascript jquery

4
推荐指数
1
解决办法
573
查看次数

标签 统计

html ×1

javascript ×1

jquery ×1