javascript 三種方法實(shí)現(xiàn)獲得和設(shè)置以及移除元素屬性
更新時(shí)間:2013年03月20日 15:55:43 作者:
獲得和設(shè)置以及移除元素屬性在操作dom的過程中會(huì)經(jīng)常遇到吧,為了提高工作的效率本文整理了一些快捷操作方法和大家一起分享,感興趣的朋友可以參考下哈
以下面的html為例
<div id="myDiv" class="bd" title="我是div">
<img id="img1" />
<a id="myA" >百度</a>
</div>
1.通過HTMLElement類型(對(duì)象)的屬性獲得和設(shè)置元素特性
var div = document.getElementById("myDiv");
var img = document.getElementById("img1");
var a = document.getElementById("myA");
//取得元素特性
alert(div.id); //"myDiv"
alert(div.className); //"bd",這里不是div.class,是因?yàn)閏lass是保留關(guān)鍵字
alert(div.title); //"我是div"
alert(a.href); //http://www.baidu.com
//設(shè)置元素特性
div.id = "myDiv2"; //id改為"myDiv2"
div.className = "ft"; //class改為"ft",如果存在名為"ft"的樣式,會(huì)立刻變?yōu)?ft"樣式,瀏覽器會(huì)立刻反應(yīng)出來
div.title = "我是myDiv2"; //title改為"我是myDiv2"
div.align = "center"; //設(shè)置居中對(duì)齊
img.src ="images/img1.gif"; //設(shè)置圖片路徑
a.innerHTML ="新浪"; //"百度"改為"新浪"
a.; //重新設(shè)置超鏈接
2.通過getAttribute()、setAttribute()和removeAttribute() 方法,獲取、設(shè)置、移除元素的特性(不推薦使用,前兩個(gè)方法IE6,7中有異常,第三個(gè)方法IE6不支持,設(shè)置自定義特性時(shí)可以使用)
getAttribute() 方法,用來獲取元素特性。接受一個(gè)參數(shù),即要獲得元素的特性名
setAttribute() 方法,用來設(shè)置元素特性。接受兩個(gè)參數(shù),即要獲得元素的特性名和特性值
removeAttribute() 方法,用來移除元素的特性。接受一個(gè)參數(shù),即要移除元素的特性名
var div = document.getElementById("myDiv");
var img = document.getElementById("img1");
var a = document.getElementById("myA");
//取得元素特性
alert(div.getAttribute("id")); //"myDiv"
alert(div.getAttribute("class")); //"bd",注意這里是class,而不是className,與上面不同
alert(div.getAttribute("title")); //"我是div"
alert(a.getAttribute("href")); //http://www.baidu.com
//設(shè)置元素特性
div.setAttribute("id","myDiv2"); //id改為"myDiv2"
div.setAttribute("class","ft"); //class改為"ft",這里同樣是class,而不是className
div.setAttribute("title","我是myDiv2"); //title改為"我是myDiv2"
div.setAttribute("align","center"); //設(shè)置居中對(duì)齊
img.setAttribute("src","images/img1.gif"); //設(shè)置圖片路徑
//移除元素特性
div.removeAttribute("class"); //移除class特性
3.通過attributes屬性,獲取、設(shè)置、移除元素的特性
var div = document.getElementById("myDiv");
//取得元素特性
alert(div.attributes["id"].nodeValue); //"myDiv"
//設(shè)置元素特性
div.attributes["id"].nodeValue = "myDiv2"; //id改為"myDiv2"
//移除元素特性
div.attributes.removeNamedItem("class"); //移除class特性
復(fù)制代碼 代碼如下:
<div id="myDiv" class="bd" title="我是div">
<img id="img1" />
<a id="myA" >百度</a>
</div>
1.通過HTMLElement類型(對(duì)象)的屬性獲得和設(shè)置元素特性
復(fù)制代碼 代碼如下:
var div = document.getElementById("myDiv");
var img = document.getElementById("img1");
var a = document.getElementById("myA");
//取得元素特性
alert(div.id); //"myDiv"
alert(div.className); //"bd",這里不是div.class,是因?yàn)閏lass是保留關(guān)鍵字
alert(div.title); //"我是div"
alert(a.href); //http://www.baidu.com
//設(shè)置元素特性
div.id = "myDiv2"; //id改為"myDiv2"
div.className = "ft"; //class改為"ft",如果存在名為"ft"的樣式,會(huì)立刻變?yōu)?ft"樣式,瀏覽器會(huì)立刻反應(yīng)出來
div.title = "我是myDiv2"; //title改為"我是myDiv2"
div.align = "center"; //設(shè)置居中對(duì)齊
img.src ="images/img1.gif"; //設(shè)置圖片路徑
a.innerHTML ="新浪"; //"百度"改為"新浪"
a.; //重新設(shè)置超鏈接
2.通過getAttribute()、setAttribute()和removeAttribute() 方法,獲取、設(shè)置、移除元素的特性(不推薦使用,前兩個(gè)方法IE6,7中有異常,第三個(gè)方法IE6不支持,設(shè)置自定義特性時(shí)可以使用)
getAttribute() 方法,用來獲取元素特性。接受一個(gè)參數(shù),即要獲得元素的特性名
setAttribute() 方法,用來設(shè)置元素特性。接受兩個(gè)參數(shù),即要獲得元素的特性名和特性值
removeAttribute() 方法,用來移除元素的特性。接受一個(gè)參數(shù),即要移除元素的特性名
復(fù)制代碼 代碼如下:
var div = document.getElementById("myDiv");
var img = document.getElementById("img1");
var a = document.getElementById("myA");
//取得元素特性
alert(div.getAttribute("id")); //"myDiv"
alert(div.getAttribute("class")); //"bd",注意這里是class,而不是className,與上面不同
alert(div.getAttribute("title")); //"我是div"
alert(a.getAttribute("href")); //http://www.baidu.com
//設(shè)置元素特性
div.setAttribute("id","myDiv2"); //id改為"myDiv2"
div.setAttribute("class","ft"); //class改為"ft",這里同樣是class,而不是className
div.setAttribute("title","我是myDiv2"); //title改為"我是myDiv2"
div.setAttribute("align","center"); //設(shè)置居中對(duì)齊
img.setAttribute("src","images/img1.gif"); //設(shè)置圖片路徑
//移除元素特性
div.removeAttribute("class"); //移除class特性
3.通過attributes屬性,獲取、設(shè)置、移除元素的特性
復(fù)制代碼 代碼如下:
var div = document.getElementById("myDiv");
//取得元素特性
alert(div.attributes["id"].nodeValue); //"myDiv"
//設(shè)置元素特性
div.attributes["id"].nodeValue = "myDiv2"; //id改為"myDiv2"
//移除元素特性
div.attributes.removeNamedItem("class"); //移除class特性
相關(guān)文章
layui 數(shù)據(jù)表格拖動(dòng) 列、行 位置重新排序功能實(shí)現(xiàn)
這篇文章主要介紹了layui數(shù)據(jù)表格拖動(dòng)列、行位置重新排序功能實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-06-06
微信小程序?qū)崿F(xiàn)根據(jù)字母選擇城市功能
這篇文章主要為大家詳細(xì)介紹了微信小程序中根據(jù)字母選擇城市的相關(guān)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
bootstrap-table formatter 使用vue組件的方法
Bootstrap table是國(guó)人開發(fā)的一款基于 Bootstrap 的 jQuery 表格插件,通過簡(jiǎn)單的設(shè)置,就可以擁有強(qiáng)大的單選、多選、排序、分頁,以及編輯、導(dǎo)出、過濾(擴(kuò)展)等等的功能。這篇文章重點(diǎn)給大家介紹bootstrap-table formatter 使用vue組件的方法,感興趣的朋友一起看看2019-05-05
js 本地預(yù)覽的簡(jiǎn)單實(shí)現(xiàn)方法
本篇文章主要是對(duì)js本地預(yù)覽的簡(jiǎn)單實(shí)現(xiàn)方法進(jìn)行了介紹,需要的朋友可以過來參考下,希望對(duì)大家有所幫助2014-02-02

