这似乎应该很容易 - 但我找不到正确的选择器。
根据文档 (http://api.jquery.com/hidden-selector/ 和 http://api.jquery.com/visible-selector/)...
元素可能因为以下原因而被视为隐藏:
祖先元素被隐藏,因此该元素未在页面上显示。
我想要检测的是"此元素可见,但包含在隐藏的父元素中"。也就是说,如果我让父元素可见,这个元素也会可见。
这似乎应该很容易 - 但我找不到正确的选择器。
根据文档 (http://api.jquery.com/hidden-selector/ 和 http://api.jquery.com/visible-selector/)...
元素可能因为以下原因而被视为隐藏:
祖先元素被隐藏,因此该元素未在页面上显示。
我想要检测的是"此元素可见,但包含在隐藏的父元素中"。也就是说,如果我让父元素可见,这个元素也会可见。
如果这是你经常使用的东西,请创建自己的选择器 :) 这里有一个例子:
jQuery.expr[':'].hiddenByParent = function(a) {
return jQuery(a).is(':hidden') && jQuery(a).css('display') != 'none';
};
你可以像这样使用它,测试标记:
<div style="display: none" id="parent">
<div>
<div id="child">Test</div>
</div>
</div>
使用示例:
$("div:hiddenByParent").length; // "2" (plain div + child match)
$("#child").is(":hiddenByParent"); // true
或者,您可以像这样使用 .filter()
函数:
$('selector').filter(function() {
return $(this).is(':hidden') && $(this).css('display') != 'none';
}
现在jQuery已经内置了这一功能。
$("#child").closest(':hidden').length == 0
$('#element').css('display') != 'none';
$('parent-selector:hidden').find('node-selector').each(function(){
if($(this).css('display') != 'none') {
// do what you wanted
}
});
return $(a).is(':hidden') && $(a).css('display') != 'none' && $(a).css('visibility') == 'visible';
。 - Mottie