首页 > jquery的:hidden选择器为什么会选中<select>标签

jquery的:hidden选择器为什么会选中<select>标签

<html>
<head>

<title>forth.html</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="../jquery.min.js"></script>

</head>
<body>

 <form action="#" id="form1">
   <select>
     <option>Option</option>
   </select>
   <input type="hidden"/><div style="display:none">test</div>
 </form>

</body>
<script type="text/javascript">

alert($("#form1 :hidden").length);

</script>
</html>
为什么结果是3,而不是2呢?谢谢。


console.log($("#form1 :hidden"))一下就知道了。选中的不是<select>,是里面的<option>

[option, input, div]

改为用$("#form1 > :hidden")来选择就好了,这样就只会选择表单下的直接子元素,因此不会选择option


select没有结束标签?


@Terry_139061 说的对,因为你选的是隐藏的标签,而option就是隐藏的标签。。select没有隐藏所以不会选中,

【热门文章】
【热门文章】