How to dynamically change CSS class of DIV tag?

前端 未结 6 1211
离开以前
离开以前 2020-12-15 04:55

I am using JavaScript. I have a variable var boolVal that either evaluates to true/false. On my page, I have a div tag.

<
相关标签:
6条回答
  • 2020-12-15 05:16

    You can add a css class based on id dynamically as follows:

    document.getElementById('idOfElement').className = 'newClassName';
    

    or using classList

    document.getElementById('idOfElement').classList.add('newClassName');
    

    Alternatively you can use other DOM methods like

    • querySelector
    • querySelectorAll
    • getElementsByClassName
    • getElementsByTagName

    etc to find elements. The last three return a collection so you'll have to iterate over it and apply the class to each element in the collection.


    In your case

    var element = document.getElementById('div1');
    if(boolVal)
       element.className= 'blueClass'; // += ' blueClass'; to keep existing classes
    else
       element.className= 'redClass';
    
    0 讨论(0)
  • 2020-12-15 05:17

    Something like this:

    document.getElementById("MyElement").className = boolVal ? "redClass" : "blueClass";
    
    0 讨论(0)
  • 2020-12-15 05:18

    First of all, AddClass is not a pure Javascript method. It's related to jQuery.

    You can use Javascript for adding a class:

    setAttribute and className both method are used to set class (class attribute), these method are not used to adding another class with existing one.

    document.getElementById('div1').setAttribute( "class", "blueClass" );
    

    OR

    document.getElementById('div1').className="redClass";
    

    Demo Fiddle

    So if you want append a css class to an element, you can do it like this -

     document.getElementById('div1').setAttribute( "class", document.getElementById('div1').getAttribute('class') + " blueClass" );
    

    OR

     document.getElementById('div1').className +=" redClass";
    

    Note: Using this way, the same class can be added multiple times. It's only a trick to do this work using javascript, it's not a perfect solution

    OR simply use jQuery to add class -

    $("#div1").addClass("blueClass");
    

    Working Fiddle

    0 讨论(0)
  • 2020-12-15 05:24

    Use

    document.getElementById('div1').className='blueClass';
    
    0 讨论(0)
  • 2020-12-15 05:24

    You should use:

        boolVal.onchange=function(){
                    document.getElementById("MyElement").className = boolVal ? "redClass" : "blueClass";
        };
    
    0 讨论(0)
  • 2020-12-15 05:26

    You can edit inline styles on a DOM element like so (height in this example):

    document.getElementById("element").style.height = height + "px";
    

    Basic syntax is:

    document.getElementById("div1").style.[style property] = [value];
    

    Syntax for the style property can vary and might not be exactly what it is in a CSS file. For example, background-color in a CSS file would be 'backgroundColor` in the above example.

    0 讨论(0)
提交回复
热议问题