[英]Can't set z-index via CSS
<html>
<head>
<title>Sample</title>
<script>
window.onload = function()
{
alert(document.getElementById('div1').style.zIndex);
alert(document.getElementById('div2').style.zIndex);
}
</script>
<style type="text/css" media="screen">
#div2
{
z-index: 1;
}
</style>
</head>
<body>
<div id="div1" style="z-index:1"></div>
<div id="div2"></div>
</body>
The outcome of the above code is "1" for the 1st alert, and nothing for the 2nd. 上面代码的结果对于第一个警报是“1”,对于第二个警告没有任何结果。
How come i can't set the z-index of "div2" via CSS? 为什么我不能通过CSS设置“div2”的z-index?
Thanks. 谢谢。
1: it is set, but JS cant determine it if it's not set inline 1:它已设置,但JS无法确定它是否未设置为内联
2: it would'nt have any effect, if the object is not positioned(as gulbrandr posted) 2:如果对象没有定位(如gulbrandr发布的话),它不会有任何影响
regarding to 1. If it's not set inline(or by javascript), you can retrieve it using currentStyle or getComputedStyle what gives you the currently applied style. 关于1.如果它没有设置为内联(或通过javascript),您可以使用currentStyle或getComputedStyle检索它,它会为您提供当前应用的样式。
From W3Schools.com : 来自W3Schools.com :
z-index only works on positioned elements (position:absolute, position:relative, or position:fixed)
z-index仅适用于定位元素(位置:绝对,位置:相对或位置:固定)
The .style.*
set of properties map directly on to properties set via the style
attribute , not those which are cascaded from a proper stylesheet. .style.*
属性集直接映射到通过style
属性设置的属性 ,而不是从适当的样式表级联的属性 。
To find the computed z-index, you need to use getComputedStyle 要查找计算的z-index,需要使用getComputedStyle
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.