Jquery:检查 div 是否包含文本,然后操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/902597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 10:05:27  来源:igfitidea点击:

Jquery: Checking to see if div contains text, then action

jquery

提问by Ayman Hourieh

I'm trying to check in jQuery if a div contains some text, and then add a class if it does.

如果 div 包含一些文本,我正在尝试检查 jQuery,如果包含,则添加一个类。

So I wrote something like this:

所以我写了这样的东西:

    if( $("#field > div.field-item").text().indexOf('someText') = 0) {
        $("#somediv").addClass("thisClass");
    }

I'm not getting this to work.

我没有得到这个工作。

<div id="field"><div class="field-item">someText</div></div>

<div id="somediv"></div>

Is this incorrect?

这不正确吗?

回答by Ayman Hourieh

Your code contains two problems:

您的代码包含两个问题:

  • The equality operator in JavaScript is ==, not =.
  • jQuery.text()joins all text nodes of matched elements into a single string. If you have two successive elements, of which the first contains 'some'and the second contains 'Text', then your code will incorrectly think that there exists an element that contains 'someText'.
  • JavaScript 中的相等运算符是==,不是=
  • jQuery.text()将匹配元素的所有文本节点连接成一个字符串。如果您有两个连续的元素,其中第一个包含'some',第二个包含'Text',那么您的代码将错误地认为存在一个包含 的元素'someText'

I suggest the following instead:

我建议改为:

if ($('#field > div.field-item:contains("someText")').length > 0) {
    $("#somediv").addClass("thisClass");
}

回答by Shaharia Azam

Yes, I now made think for me. And it works fine!!!

是的,我现在为我考虑。它工作正常!!!

if($("div:contains('CONGRATULATIONS')").length)
                        {
                            $('#SignupForm').hide(500);
                        }

回答by Tracker1

if( $("#field > div.field-item").text().indexOf('someText') >= 0)

Some browsers will include whitespace, others won't. >=is appropriate here. Otherwise equality is double equals ==

有些浏览器会包含空格,有些则不会。 >=这里很合适。否则相等是双重相等==

回答by Canavar

Ayman is right but, you can use it like that as well :

Ayman 是对的,但是,您也可以这样使用它:

if( $("#field > div.field-item").text().indexOf('someText') >= 0) {
        $("#somediv").addClass("thisClass");
    }

回答by Tim

Why not simply

为什么不简单

var item = $('.field-item');
for (var i = 0; i <= item.length; i++) {
       if ($(item[i]).text() == 'someText') {
             $(item[i]).addClass('thisClass');
             //do some other stuff here
          }
     }

回答by TM.

You might want to try the containsselector:

您可能想尝试contains选择器:

if ($("#field > div.field-item:contains('someText')").length) {
    $("#somediv").addClass("thisClass");
}

Also, as other mentioned, you must use == or === rather than =.

此外,正如其他人提到的,您必须使用 == 或 === 而不是 =。

回答by Ludolfyn

Here's a vanilla Javascript solution in 2020:

这是 2020 年的普通 Javascript 解决方案:

const fieldItem = document.querySelector('#field .field-item')
fieldItem.innerText === 'someText' ? fieldItem.classList.add('red') : '';