Changing button color programmatically

后端 未结 7 1706
一整个雨季
一整个雨季 2020-12-16 10:13

Is there a way to change the color of a button, or at least the color of the button label programmatically? I can change the label itself with

document.getE         


        
相关标签:
7条回答
  • 2020-12-16 10:29

    I believe you want bgcolor. Something like this:

    document.getElementById("button").bgcolor="#ffffff";
    

    Here are a couple of demos that might help:

    Background Color

    Background Color Changer

    0 讨论(0)
  • 2020-12-16 10:36

    If you assign it to a class it should work:

    <script>
      function changeClass(){
        document.getElementById('myButton').className = 'formatForButton';
      }
    </script>
    
    <style>
      .formatForButton {
        background-color:pink;
       }
    </style>
    
    <body>
      <input id='myButton' type=button class=none value='Change Color to pink' onclick='changeClass()'>
    </body>
    
    0 讨论(0)
  • 2020-12-16 10:43

    Probably best to change the className:

    document.getElementById("button").className = 'button_color';
    

    Then you add a buton style to the CSS where you can set the background color and anything else.

    0 讨论(0)
  • 2020-12-16 10:44

    I have finally found a working code - try this:

    document.getElementById("button").style.background='#000000';
    
    0 讨论(0)
  • 2020-12-16 10:44

    Try this code You may want something like this

    <button class="normal" id="myButton" 
            value="Hover" onmouseover="mouseOver()" 
            onmouseout="mouseOut()">Some text</button>
    

    Then on your .js file enter this.Make sure your html is connected to your .js

    var tag=document.getElementById("myButton");
    
    function mouseOver() {
        tag.style.background="yellow";
    };
    function mouseOut() {
        tag.style.background="white";
    };
    
    0 讨论(0)
  • 2020-12-16 10:45
    use jquery :  $("#id").css("background","red");
    
    0 讨论(0)
提交回复
热议问题