How can you determine if a css class exists with Javascript?

前端 未结 10 1020
闹比i
闹比i 2020-11-27 04:14

Is there a way to determine whether or not a css class exists using JavaScript?

10条回答
  •  余生分开走
    2020-11-27 04:39

    Add this Condition Above

    if (!document.getElementsByClassName('className').length){
        //class not there
    }
    else{
    //class there
    }
    

    If want to check on a element Just use

    element.hasClassName( className );
    

    also you can use on a ID

    document.getElementById("myDIV").classList.contains('className');
    

    Good Luck !!!

提交回复
热议问题