元素属性的添加删除(原生js)

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-1 09:47   128   0

添加属性

   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/fengye06050/p/8148688.html

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP