简体   繁体   中英

Changing backgroundColor after hover in javascript

I need to change hover backgroundColor in Javascript

function changeColor(color) {
var block = document.getElementsByClassName('kafelek');
for (var i = 0; i < block.length; i++) {
    block[i].style.backgroundColor = "#" + color;
}};

In this code, I change color of block after click, but i need to change color of block after hover too.

<div class="kolorek" onclick="changeColor('2ecc71');" style="background-color:#2ecc71;"></div>

Maybe try the mouseleave event:

element.addEventListener("mouseleave", function( event ) {   
    event.target.style.backgroundColor = "purple";
}

Also if you want it to change only when the mouse is on the element, you better use css :hover , like so:

element:hover {
    background-color: #yourcolor;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM