在页面中找到位置为“绝对"的所有元素 [英] find all elements in a page with position "absolute"
问题描述
我需要获取页面中所有位置为绝对"的元素.我无法使用$('*'),因为在页面中的每个元素上进行迭代都会导致性能问题.我的页面中有将近8000个元素,每次打开上下文菜单时,我都需要计算最大的z索引.我进行了搜索,但找不到有效的解决方案.我正在寻找以下提供的解决方案,
I need to get all the elements in a page with position "absolute". I could not use $('*') because iterating on each element in the page is causing performance issue. There are nearly 8000 elements in my page and each time when i open the context menu i need to calculate the maximum z index. I have searched and i could not find an efficient solution. I am looking for solution as provided below,
$(function () {
$("*[style*='position:absolute']").each (function () {
alert($(this).html());
});
});
但是,它仅在样式为内联时才有效.但是当从CSS指定位置时,我也需要所有具有绝对"位置的元素.
but, it only works if style is inline. But i need all the elements with "absolute" position when position is specified from CSS also.
请咨询.预先感谢.
推荐答案
您不能仅通过CSS属性>>来简单地选择元素>>,而不必遍历所有计算出的DOM元素样式,因为它们不是selectors
.
You cannot that simply select an element >> by just a CSS property >> without iterating all over your computed DOM element styles, cause those are not selectors
.
是的,恐怕是*
全球.而且非常慢.但是为了使其更快,请首先收集您的元素,然后将方法应用于您的元素集合:
So yes, *
global I'm afraid. And pretty slow. But to make it faster, collect first your elements, and afterwards apply a method to your elements collection:
jsFiddle demo
var absElements = []; // Elements collector
$("*").css("position", function(i, pos) {
if(pos==="absolute") absElements.push(this); // Collect elements
});
// Now once the above is done...
$(absElements) // Do whatever you want with them
这篇关于在页面中找到位置为“绝对"的所有元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!