获取每个div取决于文本的文本的值 [英] Get value of text inside each div depending text

查看:64
本文介绍了获取每个div取决于文本的文本的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的结构:

 < table id =thetable> 
< tr>
< td>
< div>
< div>一些文字< / div>
< div>
< div>< / div>
< div>(这段文字很好)< / div>
< div>一些text2< / div>
< div>一些text3< / div>
< / div>
< / div>
< / td>
< tr>
< / tr>
< td>
< div>
< div>一些文字< / div>
< div>
< div>< / div>
< div>(这段文字很好)< / div>
< div>一些text2< / div>
< div>一些text3< / div>
< / div>
< / div>
< / td>
< / tr>
< tr>
< td>
< div>
< div>一些文字< / div>
< div>
< div>< / div>
< div>(这段文字很好)< / div>
< div>一些text2< / div>
< div>一些text3< / div>
< / div>
< / div>
< / td>
< / tr>
< / table>

我想把div里面的文字,但是只有带有这个词的文字:good。并更换。
所以我这样做:

  $(#thetable div:contains('good'))。每个(函数(){
尝试{
console.log($(this).text());
console.log(----------- -------------------);
} catch(ex){

}
})

但是console.log告诉我,每个日志不仅带有正确文本的div文本还有另外两个。
正在打印:

 (这段文字很好)some text2some text3 






谢谢!

基本上来说:我想要得到div的地方,我已经得到了好的单词(或任何我想要的单词),并且在这个div插入/替换文本。在所有文档中查找,或者特别是在文档的一部分中查找,例如:在表格中。

解决方案

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



一个可能的解决方案是只提取结尾元素,比如

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

演示:小提琴


I have got this structure:

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

I want to take the text inside the div, but only the text with this word: good. and replace. So I am doing this:

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

        }
    })

But the console.log is saying to me that each log is taking not only the text of the div with good text but also the other two. It is printing:

(This text is good)some text2some text3

So I want to take only the div with text good and replace this text with another text.

Thanks!

Basically: I want to get the div where I have got the word "good"(or any word I want), and in this div insert/replace the text. Find in all document or, specifically, in a part of the document, like this example: in a table.

解决方案

Because you have a div which contains those 3 divs

one possible solution is to fetch only the end elements like

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

Demo: Fiddle

这篇关于获取每个div取决于文本的文本的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆