我正在尝试使用jQuery在JavaScript中填充数组。我在<div>
中有一些<section>
个元素,而我只想要可见的<div>
元素(通过CSS属性display: block
)到阵列。
HTML:
<section id="container">
<div>shows up 1</div>
<div style="display:none">doesn't show 2</div>
<div>shows up 3</div>
<div style="display:none">doesn't show 4</div>
<div style="display:none">doesn't show 5</div>
<div>shows up 6</div>
<div>shows up 7</div>
<div>shows up 8</div>
<div style="display:none">doesn't show 9</div>
<div>shows up 10</div>
</section>
JavaScript / jQuery
var mainList = $("#container div");
目前,无论显示哪个元素,都会获取所有<div>
元素。是否有某种过滤器我可以在此调用中获取仅可见的元素?它应该只返回6个<div>
元素,表示&#34;显示&#34;在他们中间。
小提琴: http://jsfiddle.net/259chbj4/
注意:为此,我无法使用class
display: none
。我正在寻找一种只修改JavaScript的解决方案。
答案 0 :(得分:4)
您只需使用:visible选择器。
$("#container div:visible");
答案 1 :(得分:3)
尝试:
var mainList = $('#container').find("div :visible");