简体   繁体   English

鼠标单击JavaScript错误

[英]javascript on mouse click error

I can't seem to get the mousedown mouseup's to work. 我似乎无法让mousedown mouseup正常工作。 i need an object to change color on mouse down and on mouseup change back to the original. 我需要一个对象来更改鼠标向下的颜色,并在鼠标向上更改为原始颜色。 but for some reason 但由于某种原因

onload = init;

function init() {
    document.getElementsByClassName("buttons")[0].mousedown = function () {
        blue();
    };
    document.getElementsByClassName("buttons")[0].mouseup = function () {
        green();
    };
}

function green() {
    document.getElementsByClassName("buttons")[0].style.backgroundColor = "#360"
}

function blue() {
    document.getElementsByClassName("buttons")[0].style.backgroundColor = "blue"
};

Why do you need javascript to do this? 为什么您需要使用JavaScript来做到这一点? Why not just use ":active" in CSS? 为什么不在CSS中仅使用“:active”?

button{background-color: green}
button:active{background-color: blue}

Event names include on prefix: it's onmousedown and onmouseup : 事件名称包括on前缀:这是onmousedownonmouseup

function init() {
    document.getElementsByClassName("buttons")[0].onmousedown = blue;
    document.getElementsByClassName("buttons")[0].onmouseup = green;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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