节点属性的获取有三种方法
1. 对象.属性名
2. 对象[属性名]
3. obj.getAttribute(pro)
<img src="images/1.jpg" alt="" id="pic" class="one">
<script type="text/javascript">
var txt=document.getElementById('pic');
console.log(txt.src);
console.log(txt['src']);
console.log(txt.getAttribute('src'));//三个输出值是一样的
</script>
节点属性的设置有三种方法
1. 对象.属性名=值;
2. 对象[属性名]=值;
3. obj.setAttribute(pro,value)
<img src="images/1.jpg" alt="" id="pic" class="one">
<script type="text/javascript">
var txt=document.getElementById('pic');
txt.className='two';
txt[className]='two';
txt.setAttribute('class','two');//三种写法的效果一样
</script>
注意:前两种设置和获取属性的方法,均需要注意特殊情况
class ---> className
for ---> htmlFor
通过get和set方法获取和设置属性,不需要变形