-2

我有几个 JavaScript 库竞争z-index至高无上,并希望通过z-index除了对象类型之外没有任何其他属性来转储完整的元素列表。

运行以下命令只给了我一个要遍历的元素:

$('html').each(function () {
    console.log($(this).css('z-index') + ': ' + $(this).constructor);
});

我该怎么做?

4

3 回答 3

4

您现在正在遍历html元素。您要做的是使用*选择器选择所有元素:

$("*").each(function () { 
    ...
于 2013-09-25T15:20:42.277 回答
3

像这样的东西应该工作:

$("*").each(function(k, v){
    console.log(v.nodeName + " " + $(v).css("z-Index"));
})
于 2013-09-25T15:27:28.923 回答
1

这是一个解决方案,它在没有任何第三方框架的情况下读取计算的样式表:

var elems = document.querySelectorAll( '*' );

for( var i = 0, len = elems.length; i < len; i++ ) {

    var style = window.getComputedStyle( elems[i] );

    console.log( elems[i].nodeName, style.getPropertyValue( 'z-index' ) );

    /* style['z-index'] will also work, but it is better to use the API if there is one, in case something get's changed */
}
于 2013-09-25T16:04:51.810 回答