屬性的方法操作
阿新 • • 發佈:2017-09-11
var -s com func data http 元素節點 想是 for
屬性的方法操作
var div=document.getElementById("box"); //元素節點.屬性 或(元素節點【屬性】): 綁定的屬性值不會出現在標簽上 div.index="你好";
get set/removeAttribut:綁定的屬性值會出現屬性標簽上
getAttribute() 方法返回指定屬性名的屬性值。
<div title="主題" class="abc" id="box">事實上</div> var div=document.getElementById("box"); console.log(div.getAttribute("class"))
setAttribute()方法添加指定的屬性,並為其賦指定的值 如果這個指定的屬性已經存在,則設置更改值
<div title="主題" class="abc" id="box">事實上</div> div.setAttribute("title","笑笑十年少"); console.log(div.title) div.setAttribute("index","笑"); console.log(div.getElamentById("index"))
案例
//需求:鼠標放到哪個button上,改button變成黃色背景(添加類)var but=document.getElementsByTagName("button"); for(var i=0; i<but.length; i++){ //每次循環綁定一個屬性,屬性值是該盒子的索引值 //but[i].setAttribute("index",i); but[i].index=i;//綁定一個index屬性 but[i].onmouseover=function(){ //排他思想(幹掉所有人,剩下我一個) //排他思想是和for循環連用 for(var j=0; j<but.length; j++){ but[j].className="" } this.className="current"; alert(this.index) } }
屬性的方法操作