admin管理员组

文章数量:1315286

Hey i need a list item to change colors depending on an hovering over it, but it is not working

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
</head>
<body>
    <script>
        var m = document.getElementById("Q");
        m.addEventListener("mouse over", function(){
            m.setAttribute("style", "background-color: #ccc;");
        }, false);
    </script>
    <li>1</li>
    <li id = "Q">Hello darkness my old friend</li>
    <li>2</li>
    </body>
</html>

I know I can use css to change backgound color

Hey i need a list item to change colors depending on an hovering over it, but it is not working

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
</head>
<body>
    <script>
        var m = document.getElementById("Q");
        m.addEventListener("mouse over", function(){
            m.setAttribute("style", "background-color: #ccc;");
        }, false);
    </script>
    <li>1</li>
    <li id = "Q">Hello darkness my old friend</li>
    <li>2</li>
    </body>
</html>

I know I can use css to change backgound color

Share Improve this question edited Apr 17, 2019 at 12:57 ace asked Apr 17, 2019 at 12:02 aceace 331 gold badge2 silver badges7 bronze badges 5
  • 7 Javascript seems a bit overkill here. Just use css like this #q:hover{background-color: red;} – Joscha Commented Apr 17, 2019 at 12:04
  • w3schools./jsref/met_element_setattribute.asp – Dani Commented Apr 17, 2019 at 12:05
  • 2 You talk about hover, but bind to click? – BenM Commented Apr 17, 2019 at 12:05
  • 2 It would help if the style value you provide were valid. #red is not a colour value. – Niet the Dark Absol Commented Apr 17, 2019 at 12:07
  • 2 Possible duplicate of Changing backgroundColor after hover in javascript – Bob Commented Apr 17, 2019 at 12:09
Add a ment  | 

7 Answers 7

Reset to default 7

This is something you can achieve using simple CSS:

#q:hover {
    background-color: red;
}

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
</head>
<body>
    <li>1</li>
    <li id = "Q">Hello darkness my old friend</li>
    <li>2</li>
     <script>
        var m = document.getElementById("Q");
        m.addEventListener("mouseover", function(){
            m.style.backgroundColor = "#ccc";
        }, false);
    </script>
    </body>
</html>

Here is my solution using pure js and HTML.

  • onmouseover event is triggered once u enter the element
  • onmouseleave event is triggered once u leave the element

-> you just change styles once events are triggered.

It is possible also to register listeners first to an element in js, but the solution I post has probably cleaner and clearer code.

Element event calls function. Function changes element style. Easy, peazy but I remend to use css pseudostyle.

function mouseEntered() {
  document.getElementById("Q").style.backgroundColor="pink";
};

function mouseLeaved() {
  document.getElementById("Q").style.backgroundColor="white";
};
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
</head>
<body>
    <li>1</li>
    <li onmouseover="mouseEntered()" onmouseleave="mouseLeaved()" id="Q">Hello    darkness my old friend</li>
    <li>2</li>
</body>
</html>

Just remove the # from #red and change "click" event to "mouseover" then it should work

<script>
    var m = document.getElementById("Q");
    m.addEventListener("mouseover", function(){m.setAttribute("style", "background-color: red;");}, false);
</script>

You can use the setAttribute to change the colour of the element

var myElement = document.getElementById("YOUR_ELEMENTS_UNIQUE_ID");

myElement.setAttribute('style', 'color: red');

If you want to add an effect when the user is hovering the item (and remove it when the user is NOT hovering it - aka 'on hover' effect), just go with CSS:

#Q:hover {
    background-color: red;
}

If you want to add a 'permanent' effect, after the user has hovered an item, go with JS:

var elem = document.getElementById('Q');
elem.addEventListener('onmouseenter', function(){this.style.backgroundColor = 'red'});

The above function can be improved to fire only once, and it's a nice exercise for memory management:

var elem = document.getElementById('Q');

function changeColor(e) {
    e.target.style.backgroundColor = 'red';
    e.target.removeEventListener('onmouseenter', changeColor);
};

elem.addEventListener('onmouseenter', changeColor);

It can also be applied for multiple elements:

var targets = document.getElementsByClassName('myClassName');

function changeColor(e) {
    e.target.style.backgroundColor = 'red';
    e.target.removeEventListener('onmouseenter', changeColor);
};

targets.map(target => target.addEventListener('onmouseenter', changeColor));

Alternatively, you can use the style object to do this.

var m = document.getElementById("Q");
m.addEventListener("click", function(){
    m.style.backgroundColor= '#000'
});

You can use jquery function to change color of element

<script>
$(document).ready(function(){
  $("p").mouseover(function(){
    $("p").css("background-color", "yellow");
  });
  $("p").mouseout(function(){
    $("p").css("background-color", "lightgray");
  });
});
</script>

HTML code :

<p>Move the mouse pointer over this paragraph.</p>

本文标签: htmlJavascript code to change color attribute of an elementStack Overflow