單個的點擊展示下拉菜單比較簡單。比如點擊右上角的設置按鈕會彈出修改密碼和退出功能等。 多個的點擊式展示下拉菜單,在之前有發過。 ...
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <style> .container { overflow: hidden; background-color: #333; font-family: Arial; } .container a { float: left; font-size: 16px; color: white; text-align: center; padding: 14px 16px; text-decoration: none; } .dropdown { float: left; overflow: hidden; } .dropdown .dropbtn { cursor: pointer; font-size: 16px; border: none; outline: none; color: white; padding: 14px 16px; background-color: inherit; } .container a:hover, .dropdown:hover .dropbtn { background-color: red; } .dropdown-content { display: none; position: absolute; background-color: #f9f9f9; min-width: 160px; box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); z-index: 1; } .dropdown-content a { float: none; color: black; padding: 12px 16px; text-decoration: none; display: block; text-align: left; } .dropdown-content a:hover { background-color: #ddd; } .show { display: block; } </style> </head> <body> <div class="container"> <a href="#home">主頁</a> <a href="#news">新聞</a> <div class="dropdown"> <button class="dropbtn" onclick="myFunction()">下拉菜單</button> <div class="dropdown-content" id="myDropdown"> <a href="#">Link 1</a> <a href="#">Link 2</a> <a href="#">Link 3</a> </div> </div> </div> <h3>導航欄中的下拉菜單</h3> <p>點擊按鈕顯示下拉菜單</p> </body> </html> <script> /* 點擊按鈕,下拉菜單在 顯示/隱藏 之間切換 */ function myFunction() { document.getElementById("myDropdown").classList.toggle("show"); } // 點擊下拉菜單意外區域隱藏 window.onclick = function(e) { if (!e.target.matches('.dropbtn')) { var myDropdown = document.getElementById("myDropdown"); if (myDropdown.classList.contains('show')) { myDropdown.classList.remove('show'); } } } </script>
單個的點擊展示下拉菜單比較簡單。比如點擊右上角的設置按鈕會彈出修改密碼和退出功能等。
多個的點擊式展示下拉菜單,在之前有發過。