DOM通過不同方法獲得屬性節點的值
阿新 • • 發佈:2019-01-27
<html>
<meta charset="utf-8">
<script>
function b()
{
var a = document.getElementById("id1");
}
</script>
<input id = "id1" name = "輸入" class = "class1"
test = "test1" value = "這是一個輸入框">
<br />
<button onclick="b()">這是一個按鈕</button>
<meta charset="utf-8">
<script>
function b()
{
var a = document.getElementById("id1");
var b = a.attributes;
//預設屬性可以直接獲取
document.write(a.id);document.write("<br />");
//通過屬性節點陣列獲取(位置0代表id的屬性節點)
document.write(b[0].nodeValue);document.write("<br />");
//自定義屬性可以通過下面兩種方式獲取
// 1
document.write(a.getAttribute("id"));document.write("<br />");
// 2
document.write(a.attributes["id"].nodeValue);}
</script>
<input id = "id1" name = "輸入" class = "class1"
test = "test1" value = "這是一個輸入框">
<br />
<button onclick="b()">這是一個按鈕</button>
</html>
//一個不錯的模仿天貓前端練手專案
http://how2j.cn/k/tmall-front/tmall-front-790/790.html?p=31723點選開啟連結