我正在尝试向所有设置了position:fixed的元素添加一些jQuery代码。这种操作是否可行?如果可以的话会很有帮助,这样我就不必逐个地在我的代码中为固定的对象添加额外的类了。
我正在尝试向所有设置了position:fixed的元素添加一些jQuery代码。这种操作是否可行?如果可以的话会很有帮助,这样我就不必逐个地在我的代码中为固定的对象添加额外的类了。
这个应该涵盖所有情况:
$('*').filter(function() {
return $(this).css("position") === 'fixed';
});
不如qwertymk的答案快,但如果CSS属性从另一个规则继承,则也可以正常工作,这在此处演示。
$('*').filter(function(){ return this.style && this.style.position === 'fixed'; });
更多关于 jQuery filter() 的内容
this.style
和 this.style.position==='fixed'
时会返回什么? - HalfWebDevthis.style
是undefined
,则this.style.position
将会抛出一个错误“Cannot read property position of undefined”。由于编译器从左到右进行检查,如果this.style === undefined
,它将在&&
之前失败,而不会导致错误。 - Dave Mackintoshdisplay:none
和其他display
属性,您可以在通常的jQuery选择器中使用CSS选择器:visible
,如下所示:$('.items:visible')
$('.items:hidden')