Eoch

时间不等你 做你想做的事

javascript 操作元素属性的方法

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>document</title>
<style>
</style>
<script type="text/javascript">
    
    window.onload = function() {
        
        oText1 = document.getElementById('text1');
        var name = 'value';
        
        //alert(oText1[name]);
        //alert(oText1['_name_']);
        /*
            元素.getAttribute(属性名称); 方法 获取指定元素的指定属性的值
            自定义属性用getAttribute来获取
        */
        //alert(oText1.getAttribute('value'));
        
        /*
            元素.setAttribute(属性名称); 方法 给指定元素指定的属性设置值
        */
        //oText1.setAttribute('value','呀');
        
        /*
            元素.removeAttribute(属性名称);方法 移除指定的元素的指定属性
            不要用removeAttribute删除元素自带属性
        */
        //oText1.removeAttribute('value');
        
        /*
            1.用“.”和“[]”的形式无法操作元素的自定义属性
                getAttribute可以操作元素的自定义属性
            2.可以获取元素属性的实际值
                ie7下还是会返回资源的绝对路径
        */
        //alert(oText1._name_);
        //alert(oText1['_name_']);
        //oText1.removeAttribute('_name_');
    }
    
</script>
</head>

<body id="body">
    <input type="text" id="text1" value="ABC" _name_="m味" />
</body>
</html>

 

posted @ 2015-07-28 10:13  这里来,到那里去  阅读(153)  评论(0编辑  收藏  举报