Javascript - check if div contains a word?

后端 未结 8 1749
情话喂你
情话喂你 2020-12-25 11:32

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

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

           


        
8条回答
  •  长情又很酷
    2020-12-25 11:56

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

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

    I would recommend to use indexOf.

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

提交回复
热议问题