2010-09-03 73 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