我在 jquery 中使用 .is(':visible')
方法,但它没有按预期工作。
我错过了什么?
HTML:
<div class="str">
<ul><li>1</li><li><a href="#">hide</a></li></ul>
<ul><li>2</li><li><a href="#">hide</a></li></ul>
<ul><li>3</li><li><a href="#">hide</a></li></ul>
<ul><li>4</li><li><a href="#">hide</a></li></ul>
<ul><li>5</li><li><a href="#">hide</a></li></ul>
<ul><li>6</li><li><a href="#">hide</a></li></ul>
<div class="length"></div>
</div>
jQuery:
$(function(){
$('.str ul').find('a').live('click',function(){
$(this).closest('li').parent().hide();
var ll= $('.str').find('ul').each(function(){
$('.length').text( $('.str').find('ul').is(':visible').length );
});
});
});
最佳答案
使用:$('.str').find('ul:visible').length
jsFiddle demo
$(function(){
$('.str ul').on('click','a',function(){
$(this).closest('li').parent().hide();
var visibleUL = $('.str').find('ul:visible').length;
$('.length').text( visibleUL );
alert(visibleUL );
});
});
当 .is()
返回一个 bool 值(true/false
)时,:visible
选择器以所需元素为目标创建一个元素数组集合 - 返回有效数组所需的正是length
关于javascript - .is (':visible' ) 在 jQuery 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12034517/