如何在 jQuery 中检查元素是否隐藏?

jqueryweb developmentfront end technology更新于 2026/2/10 10:52:17

使用 jQuery,您可以使用 is(:visible) 检测页面中特定元素是否隐藏或可见。您可以尝试运行以下代码来了解如何在 jQuery 中检查元素是否隐藏 −

示例

<html>
   <head>
      <title>jQuery Selector</title>
      <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
      <script>
         $(document).ready(function() {
         $("#button1").click(function(){

           var visible = $('#myElement').is(':visible');
         
           if(visible) {
              alert("input element is visible");        
           }
           else
           {
              alert("input element is hidden");
           }
         });
        });
         
      </script>
   </head>
   <body>
      <input type="text" id="myElement" style="display:block;"/>
      <button id="button1">Check Visibility</button>
   </body>
</html>

相关文章