Javascript - 检查div是否包含单词?

时间:2021-04-11 01:44:06

How can I check if a div contains a certain word?

如何检查div是否包含某个单词?

var divs= document.getElementsByTagName('div');
for (var i = 0, len = divs.length; i < len; ++i) {

    if (divs[i].text = '*word*'){
    //do somthing
}
}

doesn't work.

不起作用。

8 个解决方案

#1


31  

use the indexOf function

使用indexOf函数

if(divs[i].innerHTML.indexOf("word") !== -1) {
    // something
}

#2


5  

Use includes():

使用include():

node.textContent.includes('Some text');

#3


3  

if (document.getElementById('divId').innerHTML.indexOf("word") != -1) { }

#4


1  

Try the String.indexOf() function: if (divs[i].text.indexOf('word') != -1) {

尝试使用String.indexOf()函数:if(divs [i] .text.indexOf('word')!= -1){

#5


1  

You have to use a comparison operator not assign a variable.

您必须使用比较运算符而不分配变量。

if (divs[i].text == '*word*'){

I would recommend to use indexOf.

我建议使用indexOf。

if (divs[i].text.indexOf('*word*') != -1){

#6


1  

In addition to what others said about using .indexOf() function, I'd like to say .text is not a div node property. User .innerHTML

除了其他人所说的使用.indexOf()函数之外,我想说.text不是div节点属性。用户.innerHTML

if (divs[i].innerHTML.indexOf('word') > -1){}

#7


1  

Gosh, so many answers!

天哪,这么多答案!

To get just the text of an element, the simple way is to use textContent or, where not supported, innerText. All browsers in use support one or the other (maybe both). You can also use a regular expression (indexOf works too, a RegExp is just an option) so:

要获得元素的文本,简单的方法是使用textContent,或者在不支持的情况下使用innerText。所有使用的浏览器都支持其中一个(可能是两个)。您也可以使用正则表达式(indexOf也适用,RegExp只是一个选项)所以:

var re = new RegExp('*' + word + '*');

if (re.test(div[i].innerText || div[i].textContent)) {
  // div[i] contains /*word*/
} 

A more robust solution would be like:

一个更强大的解决方案是:

function getText(el) {
  if (typeof el.textContent == 'string') {
    return el.textContent;
  }
  if (typeof el.innerText == 'string') {
    return el.innerText;
  }
}

var re = new RegExp('*' + word + '*');

if (re.test(getText(div[i]))) {
  // div[i] contains /*word*/
} 

#8


0  

use regexp:

使用正则表达式:

if ( divs[i].textContent.match ( /\bword\b/ ) ){
    //do something
}

@RobG remind me so

@RobG提醒我

if ( divs[i].innerHTML.match ( /\bword\b/ ) ){
    //do something
}

=3=

= 3 =

#1


31  

use the indexOf function

使用indexOf函数

if(divs[i].innerHTML.indexOf("word") !== -1) {
    // something
}

#2


5  

Use includes():

使用include():

node.textContent.includes('Some text');

#3


3  

if (document.getElementById('divId').innerHTML.indexOf("word") != -1) { }

#4


1  

Try the String.indexOf() function: if (divs[i].text.indexOf('word') != -1) {

尝试使用String.indexOf()函数:if(divs [i] .text.indexOf('word')!= -1){

#5


1  

You have to use a comparison operator not assign a variable.

您必须使用比较运算符而不分配变量。

if (divs[i].text == '*word*'){

I would recommend to use indexOf.

我建议使用indexOf。

if (divs[i].text.indexOf('*word*') != -1){

#6


1  

In addition to what others said about using .indexOf() function, I'd like to say .text is not a div node property. User .innerHTML

除了其他人所说的使用.indexOf()函数之外,我想说.text不是div节点属性。用户.innerHTML

if (divs[i].innerHTML.indexOf('word') > -1){}

#7


1  

Gosh, so many answers!

天哪,这么多答案!

To get just the text of an element, the simple way is to use textContent or, where not supported, innerText. All browsers in use support one or the other (maybe both). You can also use a regular expression (indexOf works too, a RegExp is just an option) so:

要获得元素的文本,简单的方法是使用textContent,或者在不支持的情况下使用innerText。所有使用的浏览器都支持其中一个(可能是两个)。您也可以使用正则表达式(indexOf也适用,RegExp只是一个选项)所以:

var re = new RegExp('*' + word + '*');

if (re.test(div[i].innerText || div[i].textContent)) {
  // div[i] contains /*word*/
} 

A more robust solution would be like:

一个更强大的解决方案是:

function getText(el) {
  if (typeof el.textContent == 'string') {
    return el.textContent;
  }
  if (typeof el.innerText == 'string') {
    return el.innerText;
  }
}

var re = new RegExp('*' + word + '*');

if (re.test(getText(div[i]))) {
  // div[i] contains /*word*/
} 

#8


0  

use regexp:

使用正则表达式:

if ( divs[i].textContent.match ( /\bword\b/ ) ){
    //do something
}

@RobG remind me so

@RobG提醒我

if ( divs[i].innerHTML.match ( /\bword\b/ ) ){
    //do something
}

=3=

= 3 =