How to use JavaScript to change div backgroundColor

前端 未结 9 1973
萌比男神i
萌比男神i 2020-11-29 05:26

The HTML below:

some title here

some content here

相关标签:
9条回答
  • 2020-11-29 05:41

    You can try this script. :)

        <html>
        <head>
        <title>Div BG color</title>
        <script type="text/javascript">
        function Off(idecko)
        {
        document.getElementById(idecko).style.background="rgba(0,0,0,0)"; <!--- Default --->
        }
        function cOn(idecko)
        {
        document.getElementById(idecko).style.background="rgb(0,60,255)"; <!--- New content color --->
        }
        function hOn(idecko)
        {
        document.getElementById(idecko).style.background="rgb(60,255,0)"; <!--- New h2 color --->
        }
        </script>
        </head>
        <body>
    
        <div id="catestory">
    
            <div class="content" id="myid1" onmouseover="cOn('myid1'); hOn('h21')" onmouseout="Off('myid1'); Off('h21')">
              <h2 id="h21">some title here</h2>
              <p>some content here</p>
            </div>
    
            <div class="content" id="myid2" onmouseover="cOn('myid2'); hOn('h22')" onmouseout="Off('myid2'); Off('h22')">
              <h2 id="h22">some title here</h2>
              <p>some content here</p>
            </div>
    
            <div class="content" id="myid3" onmouseover="cOn('myid3'); hOn('h23')" onmouseout="Off('myid3'); Off('h23')">
              <h2 id="h23">some title here</h2>
              <p>some content here</p>
            </div>
    
        </div>
    
        </body>
    <html>
    
    0 讨论(0)
  • 2020-11-29 05:43
    <script type="text/javascript">
     function enter(elem){
         elem.style.backgroundColor = '#FF0000';
     }
    
     function leave(elem){
         elem.style.backgroundColor = '#FFFFFF';
     }
    </script>
     <div onmouseover="enter(this)" onmouseout="leave(this)">
           Some Text
     </div>
    
    0 讨论(0)
  • 2020-11-29 05:46

    It's very simple just use a function on javaScript and call it onclick

       <script type="text/javascript">
                function change()
                {
                document.getElementById("catestory").style.backgroundColor="#666666";
                }
                </script>
    
        <a href="#" onclick="change()">Change Bacckground Color</a>
    
    0 讨论(0)
  • 2020-11-29 05:47

    Adding/changing style of the elements in code is a bad practice. Today you want to change the background color and tomorrow you would like to change background image and after tomorrow you decided that it would be also nice to change the border.

    Editing the code every-time only because the design requirements changes is a pain. Also, if your project will grow, changing js files will be even more pain. More code, more pain.

    Try to eliminate use of hard coded styles, this will save you time and, if you do it right, you could ask to do the "change-color" task to someone else.

    So, instead of changing direct properties of style, you can add/remove CSS classes on nodes. In your specific case, you only need to do this for parent node - "div" and then, style the subnodes through CSS. So no need to apply specific style property to DIV and to H2.

    One more recommendation point. Try not to connect nodes hardcoded, but use some semantic to do that. For example: "To add events to all nodes which have class 'content'.

    In conclusion, here is the code which I would use for such tasks:

    //for adding a css class
    function onOver(node){
       node.className = node.className + ' Hover';
    }
    
    //for removing a css class
    function onOut(node){
        node.className = node.className.replace('Hover','');
    }
    
    function connect(node,event,fnc){
        if(node.addEventListener){
            node.addEventListener(event.substring(2,event.length),function(){
                fnc(node);
            },false);
        }else if(node.attachEvent){
            node.attachEvent(event,function(){
                fnc(node);
            });
        }
    }
    
    // run this one when window is loaded
    var divs = document.getElementsByTagName("div");
    for(var i=0,div;div =divs[i];i++){
        if(div.className.match('content')){
            connect(div,'onmouseover',onOver);
            connect(div,'onmouseout',onOut);
        }
    }
    

    And you CSS whould be like this:

    .content {
        background-color: blue;
    }
    
    .content.Hover{
        background-color: red;
    }
    
    .content.Hover h2{
        background-color : yellow;
    }
    
    0 讨论(0)
  • 2020-11-29 05:53

    This one might be a bit weird because I am really not a serious programmer and I am discovering things in programming the way penicillin was invented - sheer accident. So how to change an element on mouseover? Use the :hover attribute just like with a elements.

    Example:

    div.classname:hover
    {
        background-color: black;
    }
    

    This changes any div with the class classname to have a black background on mousover. You can basically change any attribute. Tested in IE and Firefox

    Happy programming!

    0 讨论(0)
  • 2020-11-29 06:00

    Access the element you want to change via the DOM, for example with document.getElementById() or via this in your event handler, and change the style in that element:

    document.getElementById("MyHeader").style.backgroundColor='red';
    

    EDIT

    You can use getElementsByTagName too, (untested) example:

    function colorElementAndH2(elem, colorElem, colorH2) {
        // change element background color
        elem.style.backgroundColor = colorElem;
        // color first contained h2
        var h2s = elem.getElementsByTagName("h2");
        if (h2s.length > 0)
        {
            hs2[0].style.backgroundColor = colorH2;
        }
    }
    
    // add event handlers when complete document has been loaded
    window.onload = function() {
        // add to _all_ divs (not sure if this is what you want though)
        var elems = document.getElementsByTagName("div");
        for(i = 0; i < elems.length; ++i)
        {
            elems[i].onmouseover = function() { colorElementAndH2(this, 'red', 'blue'); }
            elems[i].onmouseout = function() { colorElementAndH2(this, 'transparent', 'transparent'); }
        }
    }
    
    0 讨论(0)
提交回复
热议问题