Foo*_*bis 9 css jquery jquery-selectors
我正在尝试查找具有CSS样式的所有父元素display:none.我似乎无法让它工作.这是我得到的:
var $parents = $(...).parents("[css=display:none]");
Run Code Online (Sandbox Code Playgroud)
Nic*_*ver 16
如果你想要那些实际的 display: none;(不仅可能包含在另一个中display: none;),你可以使用.filter()和.css()获取那些父母,如下所示:
var $parents = $(...).parents().filter(function() {
return $(this).css('display') == 'none';
});
Run Code Online (Sandbox Code Playgroud)
这将获取父项,然后过滤它们以仅获取该特定父项display: none;上的那些父项.
@ Nick的解决方案是实现目标的一种非常简单直接的方法.它也可能是表现最好的方法.但是,为了完整性和便利性(如果你这么做很多),也可以创建自己的选择器:
$.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();
Run Code Online (Sandbox Code Playgroud)
示例 - http://jsfiddle.net/V8CQr/.
有关创建自定义选择器的更多信息,请访问:
http://www.jameswiseman.com/blog/2010/04/19/creating-a-jquery-custom-selector/