如何使用js判断margin的auto属性? 在ie下使用obj.style.marginRight可以正确得到auto的属性,在firefox或chrome下得到的却是数字,如何能正确得到auto属性呢?使用obj.currentStyle或window.getComputedStyle得到的也是数字。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 看不懂?<html><head><title></title><script>function getObjStyle(obj, style){ if(obj.currentStyle) return obj.currentStyle[style]; else if(window.getComputedStyle) return window.getComputedStyle(obj, null)[style]; else return null;}</script><style>#test{margin-right: auto; margin-left: auto; border: #a0a0a0 1px solid; width: 800px; height: 80px;}</style></head><div id="test" onclick="alert(getObjStyle(this, 'marginRight'))">test</div></html>我这里显示:311px 直接用 .style 你那个方法是获得的计算后的margin 直接用 .style是不能获得<head>标签内的属性的,只能获得当前块内的属性<div style="margin-left: auto;" onclick="alert(this.style.marginLeft)">test</div>这里alert是auto<html><head><title></title><style>#test{margin-right: auto; margin-left: auto; border: #a0a0a0 1px solid; width: 800px; height: 80px;}</style></head><div id="test" onclick="alert(this.style.marginLeft)">test</div></html>这里alert是空的 js中匿名函数的问题 firebug怎么查看动态绑定事件 JavaScript模拟 超类 子类的问题 求助:隐藏div问题 我想用JS获取RADIO的值,然后再在另一个页面用JS取得前一页面传递过来的值,请问该如何写呢?附程序,高手进来帮帮忙! framest底部页改变顶部页表单的值 急,javascript不会调试,很简单的问题,帮我看看 关于两本书籍望解答 请问,用showModalDialog,父子窗口之间怎么传值?谢谢。 急:如何在HTML页面上给HTC文件中定义的属性赋值? 新手求答疑,谢谢大家帮忙 【编程游戏】网页版脚本输出自己。
<head>
<title></title>
<script>
function getObjStyle(obj, style)
{
if(obj.currentStyle)
return obj.currentStyle[style];
else if(window.getComputedStyle)
return window.getComputedStyle(obj, null)[style];
else
return null;
}
</script>
<style>
#test{margin-right: auto; margin-left: auto; border: #a0a0a0 1px solid; width: 800px; height: 80px;}
</style>
</head>
<div id="test" onclick="alert(getObjStyle(this, 'marginRight'))">test</div>
</html>
我这里显示:311px
<div style="margin-left: auto;" onclick="alert(this.style.marginLeft)">test</div>这里alert是auto<html>
<head>
<title></title>
<style>
#test{margin-right: auto; margin-left: auto; border: #a0a0a0 1px solid; width: 800px; height: 80px;}
</style>
</head>
<div id="test" onclick="alert(this.style.marginLeft)">test</div>
</html>这里alert是空的