Javascript - check if div contains a word?

后端 未结 8 1603
情话喂你
情话喂你 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:53

    use regexp:

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

    @RobG remind me so

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

    =3=

提交回复
热议问题