我有一组按钮。我希望当我将鼠标放在其中一个按钮上时,该特定按钮将更改其背景颜色,而所有其他按钮将保持不变。
关于如何在没有任何回发的情况下做到这一点的任何帮助。?
最佳答案
这是一个相当笨拙的方法:
<style>
.selected { background-color : red; }
</style>
<script>
var theForm = document.getElementById("someForm");
theForm.onmouseup = function(e) {
// allow for IE
if (!e) e = window.event;
var src = e.srcElement || e.target;
if (src.tagName.toLowerCase() === "input"
&& src.type.toLowerCase() === "button"){
src.className = "selected";
for (var i = 0; i < theForm.children.length; i++)
if (src != theForm.children[i])
theForm.children[i].className = "";
}
};
</script>
这假设按钮是名为“someForm”的表单的子级,并且存在具有适当颜色的“selected”类 - 显然,您将适当修改它以适合您的标记。
请注意,上面的内容应该位于 onload 处理程序中,或者脚本 block 应该位于源中元素之后的某个位置。
另请注意,onclick
比 onmouseup
更适合此目的。
关于javascript - 鼠标向上更改按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9849989/