簡體   English   中英

鼠標單擊JavaScript錯誤

[英]javascript on mouse click error

我似乎無法讓mousedown mouseup正常工作。 我需要一個對象來更改鼠標向下的顏色,並在鼠標向上更改為原始顏色。 但由於某種原因

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"
};

為什么您需要使用JavaScript來做到這一點? 為什么不在CSS中僅使用“:active”?

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

事件名稱包括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