获取子节点的三种方式
<!DOCTYPE html
>
<html
>
<head
>
<meta charset
="UTF-8">
<title
></title
>
<script type
="text/javascript">
window
.onload = function(){
var lis
= document
.getElementById("city").children
;
console
.log(lis
.length
)
lis
= document
.getElementById("city").childNodes
;
for (var i
=0;i
<lis
.length
;i
++) {
if(lis
[i
].nodeType
==1){
console
.log(lis
[i
].innerHTML
);
}
}
lis
= document
.getElementById("city").getElementsByTagName("li");
console
.log(lis
.length
)
};
</script
>
</head
>
<body
>
<div id
="total">
<p
>
你喜欢哪个城市
?
</p
>
<ul id
="city">
<li id
="bj">北京
</li
>
<li
>上海
</li
>
<li
>东京
</li
>
<li
>首尔
</li
>
</ul
>
</div
>
</body
>
</html
>
转载请注明原文地址: https://lol.8miu.com/read-22311.html