Files
ClassContent/历届学生/font-end-nine/项目练习/学生项目/冯文焌/5js/切换变色.html
2024-09-27 02:06:13 +08:00

54 lines
1.4 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button>点我</button>
<div style="display: block;" class="text"> 我会变色</div>
</body>
<script>
// 定义 button和文字
var button = $("button"), text = $(".text");
// 定义点击事件+if语句
button.onclick = function () {
if (text.style.display == "block") {
text.style.display = "none"
} else {
text.style.display = "block"
text.style.color = `hsl(${math.random() * 360},100%,50%)`
}
}
// 封装调用
function $(className) {
return document.querySelector(className)
}
// console.dir(document.querySelector("button"));
// addEventListener
// document.querySelector("button").addEventListener('click',function(){
// setTimeout(function(){
// alert('hahaha')
// },1000)
// })
// document.querySelector('button').addEventListener('click', button)
// function button() {
// setTimeout(function () {
// alert('hahaha')
// }, 1000
// )
// }
// button( function(){
// onclick('具体')
// } )
</script>
</html>