妙味——操作元素属性的几种方法

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Document</title>
<script>
window.onload=function(){
    var oTxt = document.getElementById('txt');

    // 操作元素属性的几种方法:
    // 1、        .
    // oTxt.value='123';

    // 2、        [],优点是里面元素可变
    // oTxt['value']='123';

    //3、        DOM方法
    oTxt.setAttribute('value','123');

    // setAttribute是设置属性的方法
    // getAttribute是获取属性的方法
    // removeAttribute是删除属性的方法
}
</script>
</head>
<body>
    <input type="text" name="" id="txt" />
</body>
</html>

其中:

setAttribute(名称,值)
getAttribute(名称)
removeAttribute(名称)

posted @ 2013-12-04 21:17  白小虫  阅读(161)  评论(0编辑  收藏  举报