在javascript中,attributes屬性可以獲取并返回指定元素節(jié)點(diǎn)的屬性集合,即NamedNodeMap對象;語法“元素節(jié)點(diǎn).attributes”。
本教程操作環(huán)境:windows7系統(tǒng)、javascript1.8.5版、Dell G3電腦。
attributes 屬性返回指定節(jié)點(diǎn)的屬性集合,即 NamedNodeMap對象。
提示:您可以使用 length 屬性來確定屬性的數(shù)量,然后您就能夠遍歷所有的屬性節(jié)點(diǎn)并提取您需要的信息。
語法:node.attributes
返回值:NamedNodeMap 對象,表示屬性的集合。
<!DOCTYPE html> <html> <body> <p>點(diǎn)擊按鈕來查看 button 元素?fù)碛卸嗌賹傩浴?lt;/p> <button id="myBtn" onclick="myFunction()">試一下</button> <p id="demo"></p> <script> function myFunction() { var x = document.getElementById("myBtn").attributes.length; document.getElementById("demo").innerHTML = x; } </script> </body> </html>
我們猜猜結(jié)果是多少?結(jié)果應(yīng)該是 2(button 元素的 id 和 onclick 屬性)。看看效果圖:
【推薦學(xué)習(xí):javascript高級教程】