2010-09-03 36 views

回答

14

如果你想要的是那些实际上display: none;(不只是可能在另一个display: none;包含),你可以使用.filter().css()让这些父母,这样:

var $parents = $(...).parents().filter(function() { 
        return $(this).css('display') == 'none'; 
       }); 

这得到了父母,然后过滤它们以获得只在特定父是display: none;的人。

6

@Nick's solution是实现您的目标的一个非常简单和直接的方法。这也可能是表现最好的方法。然而,为了完整和方便的缘故(如果你这样做了很多),它也可以创建自己的选择:

$.expr[':'].css = function(obj, index, meta, stack) { 
    var args = meta[3].split(/\s*=\s*/); 
    return $(obj).css(args[0]) == args[1]; 
} 

// Then we can use our custom selector, like so: 
$("#myElement").parents(":css(display=none)").show(); 

示例 - http://jsfiddle.net/V8CQr/

查看在创建自定义选择的更多信息:

http://www.jameswiseman.com/blog/2010/04/19/creating-a-jquery-custom-selector/

1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title></title> 
    <script src="Scripts/jquery-1.7.1.min.js" type="text/javascript"></script> 
    <script type="text/javascript"> 
     $(document).ready(function() { 
      $('a').parents('div[style*="display: none"], [style*="display:none"]').show(); 
     }); 
    </script> 
</head> 
<body> 
    <div class="Test" style="color: Red; display: none;"> 
     aaa <a href="#test">test</a><br /> 
    </div> 
    <div class="Test" style="color: Aqua;"> 
     bbb <a href="#test">test</a><br /> 
    </div> 
    <div class="Test" style="font-size: 15px; display: none;"> 
     ccc <a href="#test">test</a><br /> 
    </div> 
</body> 
</html> 
+0

现场演示请参阅此链接:http://jsfiddle.net/nanoquantumtech/xYdRy/ – Thulasiram 2012-04-30 13:29:55

1

你接近:

$(...).parents('[style*="display: none"]'); 

注:这确实元件上的字符串搜索属性值,所以它不是与上面显示的$(this).css(...)过滤器解决方案一样好。

The docs