Css“悬停”选择器将临时样式应用于元素,但它不是确定的:
div:hover {
background-color: red;
}
我可以用 javascript 做同样的事情,但是对于几个元素来说它有点复杂并且不可能:
var elem = document.getElementsByTagName ("div")[0];
elem.onmouseover = function () {
this.style.backgroundColor = "red";
}
elem.onmouseout = function () {
this.style.backgroundColor = "transparent";
}
有没有更好的办法 ?像这样的东西:
document.getElementsByTagName ("div")[0].ontemporarymouseover = function () { // LoL
this.style.backgroundColor = "red";
}
谢谢