摘要:<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>js控制div樣式</title> <style type="text/css"> #box{ w
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>js控制div樣式</title> <style type="text/css"> #box{ width: 100px; height: 100px; background: red; margin: 20px 80px; } </style> </head> <body> <div id="box"></div> <input type="button" name="" id="" value="改變高度" onclick="change(value)"/> <input type="button" name="" id="" value="改變寬度" onclick="change(value)"/> <input type="button" name="" id="" value="改變顏色" onclick="change(value)"/> <input type="button" name="" id="" value="重置" onclick="change(value)"/> <input type="button" name="" id="" value="隱藏" onclick="change(value)"/> <input type="button" name="" id="" value="顯示" onclick="change(value)"/> <script type="text/javascript"> var box=document.getElementById("box"); function change (value) { if (value=="改變高度") { box.style.height="400px"; } else if(value=="改變寬度"){ box.style.width="400px"; } else if (value=="改變顏色") { box.style.backgroundColor="pink"; } else if(value=="重置"){ box.style.height="100px"; box.style.width="100px"; box.style.background="red"; } else if (value=="隱藏") { box.style.display="none"; } else if (value=="顯示") { box.style.display="block"; } } </script> </body> </html>
批改老師:天蓬老師批改時(shí)間:2018-12-03 13:45:54
老師總結(jié):用事件偵聽(tīng)器,改寫(xiě)一下這個(gè)案例, addEventListener(), 試試看