我会尝试在我的页面中查找所有“绝对”元素;与jQuery我虽然这将是像查找具有位置的元素:属性
$('[position="absolute"]')
但FF 10.0.2我无法找到一个元素...
而且,我不能运行的exaple代码上http://api.jquery.com/attribute-equals-selector/ 有什么错了这个语法?
我会尝试在我的页面中查找所有“绝对”元素;与jQuery我虽然这将是像查找具有位置的元素:属性
$('[position="absolute"]')
但FF 10.0.2我无法找到一个元素...
而且,我不能运行的exaple代码上http://api.jquery.com/attribute-equals-selector/ 有什么错了这个语法?
你可以使用filter()
$('*').filter(function(){
var position = $(this).css('position');
return position === 'absolute';
});
不能使用属性等于选择,因为,因为选择将搜索带有等于绝对喜欢这个
<div position="absolute">
但在一个属性叫做定位元素的案例位置是一个CSS属性
试试这个:
$("*[style*='position:absolute']").each (function() {
alert($(this).html());
});
演示:http://jsfiddle.net/XRRbr/1/
更多信息:在Nicola的答案http://api.jquery.com/attribute-contains-selector/
不错,但是假设风格是内嵌的。 http://jsfiddle.net/MGv9X/ – 2012-03-19 13:11:45
这是可能的,但代码会很棘手。如果接受的答案已经为你工作,那么这必须是有效的答案。 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
大厦,你也可以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');
嗯,onestly,我更想知道如果有什么错误的,我的语法,而不是明确地使用过滤器... – 2012-03-19 11:45:03
@VitoDeTullio我更新我的回答,位置是CSS属性,不是属性 – 2012-03-19 11:47:10
wooops,我很笨> _ < – 2012-03-19 11:50:11