简体   繁体   中英

How to check if html element contains every class I specify in JavaScript

I want to check if some html element contains three classes, or some of them containts another two with different names. How can I do that in JavaScript? If there is not something pure js, then jquery suggestions are welcomed.

I tried matches, but that checks some of them not all With contains that doesn't work too to check if all of them are on that element :S

    for(let i = 0;i < allButtons.length;i++)
    {


          if(allButtons[i].matches('.forwardButton,try') && allButtons[i].disabled == false)
        {
            // // alert("b")
            // if(objectWithSameOrderWhenTheyAreAnswered[`arrayWithSameOrderWhenTheyAreAnswered${answersCounter}`].length != 4)
            // {
            //     objectWithSameOrderWhenTheyAreAnswered[`arrayWithSameOrderWhenTheyAreAnswered${answersCounter}`].push(answers[i])
            // }  
        }

Try the following:

 var el = document.getElementsByTagName('div')[0]; console.log(el.classList.contains('class2')); console.log(el.classList.contains('class3')); 
 <div class="class1 class2 class4"></div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM