tritue.edu.vn


Bật tắt thẻ div bằng js

chúng ta có thể ẩn/hiện thẻ div qua id bằng js



Code minh họa

Copy
         

 <script>

    function toggleById(id) {

      //alert(id);

      const element = document.getElementById(id);

      if (element.style.display === "none" || element.style.display === "") {

        element.style.display = "block";

      } else {

        element.style.display = "none";

      }

    }

  </script>


Hoặc ẩn/hiện thẻ div qua class

Copy
         

<!DOCTYPE html>
<html lang="vi">
<head>
  <meta charset="UTF-8">
  <title>Bật/Tắt thẻ div</title>
  <style>
    #myDiv {
      width: 200px;
      height: 100px;
      background-color: lightblue;
      padding: 10px;
      display: none; /* Ẩn ban đầu */
    }
  </style>
</head>
<body>

  <button onclick="toggleDiv()">Bật/Tắt div</button>

  <div id="myDiv">
    Đây là nội dung của thẻ div.
  </div>

  <script>
    function toggleDiv() {
      const div = document.getElementById("myDiv");
      if (div.style.display === "none") {
        div.style.display = "block";
      } else {
        div.style.display = "none";
      }
    }
  </script>

</body>
</html>




Link chia sẻ bài viết
Copy
            https://tritue.edu.vn/tuecode/tracnghiem30/index.php/baiviet/post/view/id/238?id=238