我想获得一个页面中的所有元素(所有div)与CSS属性position:fixed;
,并删除或更改该属性。
使用JavaScript / jQuery可以做到这点吗?
发布于 2015-07-26 05:01:07
是的,这是可能的。您可以使用document.querySelectorAll
方法选择div
元素,然后过滤具有固定位置的元素:
[].forEach.call(document.querySelectorAll('div'), function(el) {
if (window.getComputedStyle(el).position === 'fixed') {
// el.style.position = 'relative';
}
});
发布于 2015-07-26 05:04:05
是的,你可以用jquery、和函数来实现:
Js代码:
$('div').each(function(){ //loop over all the divs
//Condition for the div with style propriety position:fixed;
if($(this).css('css('position')=="fixed"){
//Do the change you want
}
})
发布于 2015-07-26 05:04:20
这当然是可以做到的,只是效率不高。@Vohuman用普通的javascript很好地实现了这一点。我推荐看一下这篇类似的文章,它也有用jQuery和Select all elements that have a specific CSS, using jQuery实现的答案
https://stackoverflow.com/questions/31631041
复制相似问题