元素属性的添加删除(原生js)
生活随笔
收集整理的這篇文章主要介紹了
元素属性的添加删除(原生js)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
添加屬性
odiv.setAttribute("title","hello div!");odiv.setAttribute("class","boxClass");odiv.setAttribute("hello","divTag");//自定義屬性設(shè)(hello="divTag")
獲取屬性
var v=odiv.getAttribute("hello");console.log(v);//divTagconsole.log(odiv["hello"]);//undefiend
刪除屬性
odiv.removeAttribute("title");//參數(shù):屬性名//路徑相關(guān)屬性var oImg = document.getElementsByTagName("img")[0];var url=oImg.getAttribute("src")console.log(url);//圖片相對(duì)路徑var url2 = oImg.src;console.log(url2);//獲取絕對(duì)路徑
更多專(zhuān)業(yè)前端知識(shí),請(qǐng)上 【猿2048】www.mk2048.com 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)
總結(jié)
以上是生活随笔為你收集整理的元素属性的添加删除(原生js)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: CSS知识点整理(2):框模型,定位
- 下一篇: js基础---数组方法