桌面端实现点击增加背景颜色,移动端事项触摸增加背景颜色,但是鼠标和手指离开后背景颜色需要复原
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
p:active {
background-color: red;
}
</style>
</head>
<body>
<p >p:active</p>
<script>
document.querySelector('p').addEventListener('touchstart', function () {
this.style.backgroundColor = 'red'
});
document.querySelector('p').addEventListener('touchend', function () {
this.style.backgroundColor = 'transparent'
});
document.querySelector('p').addEventListener('mousedown', function () {
this.style.backgroundColor = 'red'
});
document.querySelector('p').addEventListener('mouseup', function () {
this.style.backgroundColor = 'transparent'
});
</script>
</body>
</html>
浙公网安备 33010602011771号