javascript - 怎么用js写按钮点击下去时颜色变深,松开按钮时未点击前的状态?
本文介绍了javascript - 怎么用js写按钮点击下去时颜色变深,松开按钮时未点击前的状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
用onmousedown和onmouseup同时绑定统一目标元素,但是只有第一个函数起作用
尝试用其他函数绑定,依旧只有第一个函数起作用
js代码如下:
//点击中间确定按钮
function confirmBtn(){
var aCenter = document.getElementById("confirmBtn");
aCenter.onmousedown = function () {
aCenter.style.boxShadow = "inset 0 0 50px #333";
console.log( aCenter );
}
aCenter.onmouseup = function(){
// aCenter.style.boxShadow = "0 0 0 transparent";
console.log( aCenter );
}
}
confirmBtn();
解决方案
1.确定只有第一个函数有用?我这边在控制台显示你的两个函数都console出来了,把第二个函数的注释去掉也可以有类似css active的效果。
function confirmBtn(){
var aCenter = document.getElementById("confirmBtn");
aCenter.onmousedown = function () {
aCenter.style.boxShadow = "inset 0 0 50px #333";
console.log( aCenter );
}
aCenter.onmouseup = function(){
aCenter.style.boxShadow = "0 0 0 transparent";
console.log( aCenter );
}
}
confirmBtn();
2.另外这个为什么要用js模拟呢?可以用css的:active伪类做。
这篇关于javascript - 怎么用js写按钮点击下去时颜色变深,松开按钮时未点击前的状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文