使用if條件判斷,來(lái)實(shí)現(xiàn)div標(biāo)簽的顯示和隱藏效果的切換。 代碼如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> #div1{ width: 100px; height: 200px; background: green; } </style> <script> function showhide(){ var oDiv = document.getElementById('div1'); if(oDiv.style.display == 'none'){ oDiv.style.display = 'block'; }else{ oDiv.style.display = 'none'; } } </script> </head> <body> <input type="button" value="顯示隱藏" onclick="showhide()"> <div id="div1" style="display: none;"></div> </body> </html> 注意:
在函數(shù)中,要想修改標(biāo)簽的屬性的值,首先還是要先獲取這個(gè)標(biāo)簽。 |
|
來(lái)自: 悅光陰 > 《待分類(lèi)》