<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
#abc{
z-index:1;
}
</style>
<script>
function z(){
alert(document.getElementById("abc").style.zIndex);
}
</script>
</head>
<body>
<div id="abc">abc</div>
<input type="button" onclick="z();" value="zIndex">
</body>
</html>
不管有没有在样式表里设定 z-index:1
在IE里,弹出的值都是 0
在Firefox里,弹出的值都是 空。这是怎么回事呢?难道样式里的 z-index 不能影响 style.zIndex 吗?
参考,getComputedStyle在webkit解析器下似乎有问题,以下方法在chrome和safari下还有问题.仅支持ie/ff,浏览器兼容还真是个体力活.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
#abc{
z-index:1;
}
</style>
<script>
function z(){
//alert(document.getElementById("abc").style.zIndex);
var obj = document.getElementById("abc"),
prop = obj.currentStyle ? 'zIndex' : 'z-index';
alert(getCssStyle(obj,prop));
}function getCssStyle(obj,prop)
{ var p;
if(obj.currentStyle)
{//ie
p = obj.currentStyle;
return prop ? p[prop] : p;
}
else if(window.getComputedStyle)
{//firefox
p = window.getComputedStyle(obj,null);
return prop ? p.getPropertyValue(prop) : p;
}
}
</script>
</head>
<body>
<div id="abc">abc</div>
<input type="button" onclick="z();" value="zIndex">
</body>
</html>
IE DOM1还没完全实现
Moliza相对好些DOM2
为何已经设定了 position 为 absolute 了,弹出的值依然为 0 呢?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
#abc{
position:absolute;
top:100px;
left:100px;
z-index:2;
}
</style>
<script>
function z(){
alert(document.getElementById("abc").style.zIndex);
}
</script>
</head>
<body>
<div id="abc">abc</div>
<input type="button" onclick="z();" value="zIndex">
</body>
</html>
ff : window.getComputedStyle(element,null).getPropertyValue