Javascript - check if div contains a word?

后端 未结 8 1613
情话喂你
情话喂你 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){
    
    0 讨论(0)
  • 2020-12-25 12:02

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

    if (divs[i].innerHTML.indexOf('word') > -1){}
    
    0 讨论(0)
提交回复
热议问题