属性操作


添加属性

odiv.setAttribute("title","hello div!");
odiv.setAttribute("class","boxClass");
odiv.setAttribute("hello","divTag");//自定义属性设(hello="divTag")

获取属性

var v=odiv.getAttribute("hello");
console.log(v);//divTag
console.log(odiv["hello"]);//undefiend

删除属性

odiv.removeAttribute("title");//参数:属性名
 
//路径相关属性
var oImg = document.getElementsByTagName("img")[0];
var url=oImg.getAttribute("src")
console.log(url);//图片相对路径
 
var url2 = oImg.src;
console.log(url2);//获取绝对路径
原文地址:https://www.cnblogs.com/2393920029-qq/p/12555465.html