点击事件—弹窗
<button type="button" onclick="alert('欢迎!')>点我</button>
改变HTML的内容
教程地址
改变原来的内容,或者盖上一层东西
<script>function myFunction(){ //myFunction()点击的事件名 x=document.getElementById("some id"); // 找到元素 替换元素的的位置 x.innerHTML="输入要改变的内容"; // 改变内容}</script><button type="button" onclick="myFunction()">点击这里</button>
改变HTML图像,点亮灯泡
点亮灯泡
改变HTML样式
改变文字颜色
以上两个结合,来回点击改变样式
function color(){ x=document.getElementById("a"); if (x.style.color.match("blue")) //x.style.color.match("blue")检索样式中有没有blue字符串,若有则样式更新为red,反之 { x.style.color="red"; } else { x.style.color="blue"; } } </script> <p id="a" style="color:blue; " >change color</p> <button onclick="color()"> 点击</button>