JavaScript DOM操作案例點擊按鈕顯示隱藏div優化後
阿新 • • 發佈:2018-07-21
元素 clas src com 點擊事件 cti charset round dom
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <style> div { width: 300px; height: 300px; background-color: pink; } </style> </head> <body> <inputtype="button" value="隱藏" id="bt1"/> <div id="dv"></div> <script src="common.js"></script> <script> //獲取按鈕元素,註冊點擊事件,添加事件處理函數 my$("bt1").onclick = function () { //判斷當前點擊按鈕的value值 if (this.value == "隱藏") { my$("dv").style.display = "none";this.value = "顯示"; } else if (this.value == "顯示") { my$("dv").style.display = "block"; this.value = "隱藏"; } }; </script> </body> </html>
JavaScript DOM操作案例點擊按鈕顯示隱藏div優化後