38 lines
1.1 KiB
HTML
38 lines
1.1 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>
|
|
var button = $("button"), text = $(".text");
|
|
//定义一个变量存放html的button 以及class为text的div
|
|
|
|
button.onclick = function() {
|
|
//定义按钮点击事件
|
|
if (text.style.display == "block") {
|
|
text.style.display = "none"
|
|
//如果当前div显示就让它隐藏
|
|
|
|
} else {
|
|
text.style.display = "block"
|
|
text.style.color = `hsl(${Math.random() * 360},100%,50%)`
|
|
//否则就让它显示 并且设置随机颜色 random()是从0——1中随机生成一个随机数
|
|
}
|
|
}
|
|
|
|
|
|
|
|
function $(className) {
|
|
return document.querySelector(className)
|
|
}
|
|
|
|
//定义一个通用方法传递className的值
|
|
</script>
|
|
</html> |