如何仅使用jQuery选择可见元素?
jQuery选择器:可见和:隐藏只尊重显示:没有真的隐藏?不可见度:隐藏或可见度:可见。
我明白他们在技术上不是隐藏的because they still take their space.我只想知道他们的状态,所以我可以检查可见的复选框。
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>jQuery :visiblity Selector Test</title> <script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function() { $("#VisibleCount").text($("input[type=checkBox]:visible").length); //returns 3. I think it should be 2 $("#HiddenCount").text($("input[type=checkBox]:hidden").length); //returns 1. I think it should be 2 }); </script> <style type="text/css"> #TestArea { border: solid red 1px; } #Results { background-color: Lime; } .container { border: solid black 1px; } </style> </head> <body> <div id="TestArea"> <div class="container"> visibility: hidden;<div style="visibility: hidden;"> <input id="CheckBox1" type="checkBox" /> </div> </div> <div class="container"> visibility: visible;<div style="visibility: visible;"> <input id="CheckBox2" type="checkBox" /> </div> </div> <div class="container"> display: none;<div style="display: none;"> <input id="CheckBox3" type="checkBox" /> </div> </div> <div class="container"> display: inline;<div style="display: inline;"> <input id="CheckBox4" type="checkBox" /> </div> </div> </div> <div id="Results"> <div> Visible Count: <span id="VisibleCount"></span> </div> <div> Hidden Count: <span id="HiddenCount"></span> </div> </div> </body> </html>