js操作Attribute,控制項的各種屬性.....maxlength,style...

來源:互聯網
上載者:User

標籤:開始   並且   efi   hello   inpu   dev   文章   html   doc   

Attribute是屬性的意思,文章僅對部分相容IE和FF的Attribute相關的介紹。

attributes:擷取一個屬性作為對象

getAttribute:擷取某一個屬性的值
setAttribute:建立一個屬性,並同時給屬性捆綁一個值
createAttribute:僅建立一個屬性
removeAttribute:刪除一個屬性

getAttributeNode:擷取一個節點作為對象
setAttributeNode:建立一個節點
removeAttributeNode:刪除一個節點

attributes可以擷取一個對象中的一個屬性,並且作為對象來調用,注意在這裡要使用“[]”,IE在這裡可以使用“()”,考慮到相容性的問題,要使用“[]”。關於attributes屬性的使用方式上,IE和FF有巨大的分歧,在此不多介紹。

attributes的使用方法:(IE和FF通用)

<body>
<div id = "t"><input type = "hidden" id = "sss" value = "aaa"></div>
</body>
<script>
var d = document.getElementById("sss").attributes["value"];
document.write(d.name);
document.write(d.value);
//顯示value aaa
</script>

getAttribute,setAttribute,createAttribute,removeAttribute四兄弟的概念比較容易理解,使用方法也比較簡單,唯一需要注意這幾點:

1、createAttribute在使用的時候不需要基於對象的,document.createAttribute()就可以。

2、setAttribute,createAttribute在使用的時候如果是使用的時候不要使用name,type,value等單詞,IE都FF的反應都奇怪的難以理解。

3、createAttribute在使用的時候如果只定義了名字,沒有d.nodeValue = "hello";語句定義值,FF會認為是一個Null 字元串,IE認為是undefined,注意到這點就可以了。

getAttribute的使用方法:

<body>
<div id = "t"><input type = "hidden" id = "sss" value = "aaa"></div>
</body>
<script>
var d = document.getElementById("sss").getAttribute("value");
document.write(d);
//顯示 aaa
</script>

setAttribute的使用方法:(你會發現多了一個名為good的屬性hello)

<body>
<div id = "t"><input type = "hidden" id = "sss" value = "aaa"></div>
</body>
<script>
var d = document.getElementById("sss").setAttribute("good","hello");
alert(document.getElementById("t").innerHTML)
</script>

createAttribute的使用方法:(多了一個名為good的空屬性)

<body>
<div id = "t"><input type = "hidden" id = "sss" value = "aaa"></div>
</body>
<script>
var d = document.createAttribute("good");
document.getElementById("sss").setAttributeNode(d);
alert(document.getElementById("t").innerHTML)
</script>

removeAttribute的使用方法:(少了一個)

<body>
<div id = "t"><input type = "hidden" id = "sss" value = "aaa"></div>
</body>
<script>
var d = document.getElementById("sss").removeAttribute("value");
alert(document.getElementById("t").innerHTML)
</script>

getAttributeNode,setAttributeNode,removeAttributeNode三個方法的特點是都直接操作一個node(節點),removeAttributeNode在一開始的時候總會用錯,但是充分理解了node的含義的時候,就能夠應用自如了。

getAttributeNode的使用方法:

<body>
<div id = "t"><input type = "hidden" id = "sss" value = "aaa"></div>
</body>
<script>
var d = document.getElementById("sss").getAttributeNode("value"); 
document.write(d.name);
document.write(d.value);
//顯示 value aaa
</script>

setAttributeNode的使用方法:

<body>
<div id = "t"><input type = "hidden" id = "sss" value = "aaa"></div>
</body>
<script>
var d = document.createAttribute("good");
document.getElementById("sss").setAttributeNode(d);
alert(document.getElementById("t").innerHTML);
</script>

removeAttributeNode的使用方法:

<body>
<div id = "t"><input type = "hidden" id = "sss" value = "aaa"></div>
</body>
<script>
var d = document.getElementById("sss").getAttributeNode("value")
document.getElementById("sss").removeAttributeNode(d); 
alert(document.getElementById("t").innerHTML);
</script>

 

js操作Attribute,控制項的各種屬性.....maxlength,style...

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.