2012-03-19 64 views

回答

18

你可以使用filter()

$('*').filter(function(){ 
    var position = $(this).css('position'); 
    return position === 'absolute'; 
}); 

不能使用属性等于选择,因为,因为选择将搜索带有等于绝对喜欢这个

<div position="absolute"> 

但在一个属性叫做定位元素的案例位置是一个CSS属性

+0

嗯,onestly,我更想知道如果有什么错误的,我的语法,而不是明确地使用过滤器... – 2012-03-19 11:45:03

+0

@VitoDeTullio我更新我的回答,位置是CSS属性,不是属性 – 2012-03-19 11:47:10

+0

wooops,我很笨> _ < – 2012-03-19 11:50:11

0

试试这个:

$("*[style*='position:absolute']").each (function() { 
    alert($(this).html()); 
}); 

演示:http://jsfiddle.net/XRRbr/1/

更多信息:在Nicola的答案http://api.jquery.com/attribute-contains-selector/

+1

不错,但是假设风格是内嵌的。 http://jsfiddle.net/MGv9X/ – 2012-03-19 13:11:45

+0

这是可能的,但代码会很棘手。如果接受的答案已经为你工作,那么这必须是有效的答案。 JFYI, https://developer.mozilla.org/en/DOM/window.getComputedStyle http://stackoverflow.com/questions/754607/can-jquery-get-all-css-styles-associated-与-一个元件 – codef0rmer 2012-03-19 13:50:20

10

大厦,你也可以extend jQuery's selector engine

$.extend($.expr[':'],{ 
    absolute: function(el) { 
     return $(el).css('position') === 'absolute'; 
    }, 
    relative: function (el) { 
     return $(el).css('position') === 'relative'; 
    }, 
    static: function (el) { 
     return $(el).css('position') === 'static'; 
    }, 
    fixed: function (el) { 
     return $(el).css('position') === 'fixed'; 
    } 
}); 

然后,你可以,你做这样的事情。

$(':absolute');

$('div.sidebar:relative');