js获取css属性
时间:2010-08-17 来源:艾伦
<head runat="server">
<title></title>
<style type="text/css">
#btn1{ width: 330px;}
#btn2{width: 400px;}
#btn3{width: 100px;}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" id="btn1" value="1" onclick="returnStyle('btn1','width')" />
<input type="button" id="btn2" value="2" onclick="FF()" />
<input type="button" id="btn3" value="3" onclick="ie()" />
</div>
<script type="text/javascript">
//ie或者ff下执行都可以
function returnStyle(obj, styleName) {
var myObj = typeof obj == "string" ? document.getElementById(obj) : obj;
if (document.all) {
alert(eval("myObj.currentStyle." + styleName));
} else {
alert(eval("document.defaultView.getComputedStyle(myObj,null)." + styleName));
}
}
//只支持ff
function FF() {
alert(document.defaultView.getComputedStyle(document.getElementById('btn2'), null).width);
}
//只支持ie
function ie() {
alert(document.getElementById('btn3').currentStyle.width);
}
</script>
<title></title>
<style type="text/css">
#btn1{ width: 330px;}
#btn2{width: 400px;}
#btn3{width: 100px;}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" id="btn1" value="1" onclick="returnStyle('btn1','width')" />
<input type="button" id="btn2" value="2" onclick="FF()" />
<input type="button" id="btn3" value="3" onclick="ie()" />
</div>
<script type="text/javascript">
//ie或者ff下执行都可以
function returnStyle(obj, styleName) {
var myObj = typeof obj == "string" ? document.getElementById(obj) : obj;
if (document.all) {
alert(eval("myObj.currentStyle." + styleName));
} else {
alert(eval("document.defaultView.getComputedStyle(myObj,null)." + styleName));
}
}
//只支持ff
function FF() {
alert(document.defaultView.getComputedStyle(document.getElementById('btn2'), null).width);
}
//只支持ie
function ie() {
alert(document.getElementById('btn3').currentStyle.width);
}
</script>
</form>
经过分析,在ie和ff下的用法是不同的!
相关阅读 更多 +