1.节点属性的设置
对象.属性名=值
<img src="images/1.jpg" alt="" id="imgo">
<input type="text" id="inp">
var oImg=document.getElementById('imgo');
oImg.src='images/2.jpg'//对象.属性名=值
对象[‘属性名’]=值
<img src="images/1.jpg" alt="" id="imgo">
<input type="text" id="inp">
var oImg=document.getElementById('imgo');
oImg['src']='images/2.jpg'//对象['属性名']=值
对象.setAttribute(pro属性,value值)
<img src="images/1.jpg" alt="" id="imgo">
<input type="text" id="inp">
var oImg=document.getElementById('imgo');
oImg.setAttribute('src','images/2.jpg')//对象.setAttribute(pro属性,value值)
节点属性值的获取
对象.属性名
<img src="images/1.jpg" alt="" id="imgo">
<input type="text" id="inp">
var oImg=document.getElementById('imgo');
var a=oImg.src;
console.log(a)
对象[‘属性名’]
<img src="images/1.jpg" alt="" id="imgo">
<input type="text" id="inp">
var oImg=document.getElementById('imgo');
var a=oImg['src'];
console.log(a)
对象.getAttributes(pro)
<img src="images/1.jpg" alt="" id="imgo">
<input type="text" id="inp">
var oImg=document.getElementById('imgo');
var a=oImg.getAttribute('src');
console.log(a)
**注意:**前两种设置和获取属性的方法均需要特殊情况
class------>className
for---------->htmlFor
通过get和set方法获取和设置属性不需要变形(不过也有缺陷比如value和checked)
例如:
value:
<input type="text" id="inp">
var inp1=document.getElementById('inp');
var a=inp1.setAttribute('value','哈哈');
console.log(a)//结果undefined
checkbox:
<input type="checkbox" id="inpu">游戏
var inp2=document.getElementById('inpu');
inp1.setAttribute('checkbox','checked');//没作用