Javascript 查找具有负top属性的所有html DIV元素

Javascript 查找具有负top属性的所有html DIV元素,javascript,jquery,html,Javascript,Jquery,Html,这个jquery代码应该返回所有具有负top属性但不起作用的DIV $('div').filter(function(){return parseInt(($(this).css('top')) <0 )}) $('div').filter(function(){return parseInt(($(this.css('top'))尝试使用position().top而不是css('top') $('div').filter(function(){return parseInt(($(t

这个jquery代码应该返回所有具有负top属性但不起作用的DIV

$('div').filter(function(){return parseInt(($(this).css('top')) <0 )})
$('div').filter(function(){return parseInt(($(this.css('top'))尝试使用
position().top
而不是
css('top')


$('div').filter(function(){return parseInt(($(this).position().top)如果您有一些额外的参数,parseInt将围绕您的条件进行包装。请尝试以下操作:

$('div').filter(function() {
    return parseInt($(this).css('top')) < 0;
});
$('div').filter(函数(){
返回parseInt($(this.css('top'))<0;
});
$('div')。过滤器(函数(){
返回parseInt(getComputedStyle(this,null).top,10)<0);
});
这对我很有用:

$('div').filter(function(index,element){
    if (parseInt(($(element).css('top'))) < 0) return element;
});
$('div').filter(函数(索引,元素){
if(parseInt(($(element).css('top'))<0)返回元素;
});

Fiddle:

另一种选择是elem.offsetTop(return integet,不必使用parseInt)我可能错了,但你不是想在bool上调用parseInt吗?$('div').filter(function(){return parseInt($(this.css('top'))<0;})可能。在这里可以正常工作@PNS-nope,这是一个条件,如果从样式“top”解析的整数小于零,返回true,否则返回false。@PNS:发现得很好。原始代码中
parseInt()
中的代码是
($(this).css('top'))这里的问题是,通过类应用的任何
top
样式似乎都会被
this.style.top
忽略,但只有直接在行中应用的样式。请参见此。使用您的代码,它只拾取4个div中的2个,因为2个通过类应用了负top。@FrançoisWahl-您当然是对的,将其更改为使用什么jQuery使用。
css('top')
没有问题。问题是未命中的
()
。它们仍然未命中代码中的位置,并且在删除额外的
()
之前也不起作用。
$('div').filter(函数(){return parseInt($(this.position().top)<0;})
$('div').filter(function(){
    return parseInt( getComputedStyle(this, null).top, 10 ) < 0);
});
$('div').filter(function(index,element){
    if (parseInt(($(element).css('top'))) < 0) return element;
});