简体   繁体   中英

javascript on mouse click error

I can't seem to get the mousedown mouseup's to work. 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? Why not just use ":active" in CSS?

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

Event names include on prefix: it's onmousedown and onmouseup :

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

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