document.getElementById()用法详解:
此方法是javascript中最为常用的方法之一,用以取得指定id属性值的对象。
语法格式:
document.getElementById ( sID )
sID参数是必选项,它的值是一个id属性值。
此方法的的返回值是一个对象。一般来说一个文档中id属性值是唯一的,也就是说没有两个重复的属性值。
代码实例如下:
<!DOCTYPE html><html><head><meta charset="utf-8"><meta name="author" content="http://www.softwhy.com/" /><title>getElementById()用法详解-蚂蚁部落</title><style type="text/css">ul{ list-style:none;}</style><script type="text/javascript">window.onload=function(){ var myli=document.getElementById("second"); myli.style.color="red";}</script></head><body> <div> <ul> <li>蚂蚁部落一</li> <li id="second">蚂蚁部落二</li> <li>蚂蚁部落三</li> <li>蚂蚁部落四</li> </ul> </div></body></html>
代码可以通过document.getElementById("second")获取id属性值为second的对象,并且在通过style.color将其字体颜色设置红色。
原文地址是:http://www.softwhy.com/forum.php?mod=viewthread&tid=6287
更多内容可以参阅:http://www.softwhy.com/javascript/
原标题:document.getElementById()用法详解
关键词:get