function test( id )
{
alert( document.getElementById( id ).style.display );
}
Run Code Online (Sandbox Code Playgroud)
getElementById.style.display究竟返回了什么?它是一个对象还是一个值?警报根本没有显示任何内容.我没有使用纯数字作为id,它是独一无二的.
DOM方法,如document.getElementById()创建对象,指向并包含有关指定HTMLElement或元素集的某些细节.
不幸的是,该.style属性只知道使用相同功能设置的样式属性.在下面的示例中,单击what color?将无效,直到您单击为止change color.
<html>
<head>
<script>
function whatColor() {
var d = document.getElementById( 'ddd' );
alert( d.style.backgroundColor );
}
function changeColor() {
var d = document.getElementById( 'ddd' );
d.style.backgroundColor='orange';
}
</script>
<style>
#ddd {
background-color:gray;
}
</style>
</head>
<body>
<input type="button" onclick="whatColor();" value="what color?" />
<input type="button" onclick="changeColor();" value="change color" />
<div id="ddd"> </div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我建议在getComputedStyle和currentStyle(IE,当然是不同的)上阅读PKK的精彩页面http://www.quirksmode.org/dom/getstyles.html
在本教程结束时,有一个非常适合您的功能,虽然jQuery等框架确实为样式化页面元素提供了非常方便和强大的功能:http://api.jquery.com/css/
| 归档时间: |
|
| 查看次数: |
46375 次 |
| 最近记录: |