DOM – Tag/Class/Name
<head>
<meta charset="utf-8">
<title></title>
<script>
window.onload = function(){
varaLis = document.getElementsByTagName("li");
alert(aLis);
alert(aLis.length);
}
</script>
</head>
<body>
<ul>
<li>111111</li>
<li>222222</li>
<li>333333</li>
<li>444444</li>
</ul>
<ol>
<li>555555</li>
<li>666666</li>
<li>777777</li>
<li>888888</li>
</ol>
</body>
<head>
<meta charset="utf-8">
<title></title>
<script>
window.onload = function(){
var oUl = document.getElementById("a");
var aLis = oUl.getElementsByTagName("li");
//这里是重点,先找id,再找id下的标签
alert(aLis.length);
}
</script>
</head>
<body>
<ul id="a">
<li>111111</li>
<li>222222</li>
<li>333333</li>
<li>444444</li>
</ul>
<ol>
<li>555555</li>
<li>666666</li>
<li>777777</li>
<li>888888</li>
</ol>
</body>
<head>
<meta charset="utf-8">
<title></title>
<script>
window.onload = function(){
var oUl = document.getElementById("a");
var aLis = oUl.getElementsByTagName("li");
//alert(aLis.length);
for(i=0;i<aLis.length;i++){
alert(aLis[i].innerHTML);
}
}
</script>
</head>
<body>
<ul id="a">
<li>111111</li>
<li>222222</li>
<li>333333</li>
<li>444444</li>
</ul>
<ol>
<li>555555</li>
<li>666666</li>
<li>777777</li>
<li>888888</li>
</ol>
</body>
<head>
<metacharset="utf-8">
<title></title>
<script>
window.onload = function(){
varnodes = document.getElementsByName("hello");
alert(nodes.length);
}
</script>
</head>
<body>
<div name="hello"></div>
<span name="hello"></span>
<input name="hello" />
<divid="div1">
<span name="hello"></span>
</div>
</body>
<head>
<metacharset="utf-8">
<title></title>
<script>
window.onload = function(){
//var nodes = document.getElementsByName("hello");
//alert(nodes.length);
varoDiv = document.getElementById("div1");
varnodes = oDiv.getElementsByName("hello");
alert(nodes.length);
}
</script>
</head>
<body>
<div name="hello"></div>
<span name="hello"></span>
<input name="hello" />
<divid="div1">
<span name="hello"></span>
</div>
</body>
<head>
<meta charset="utf-8">
<title></title>
<script>
window.onload = function(){
var nodes = document.getElementsByClassName("box");
//alert(nodes);
alert(nodes.length);
}
</script>
</head>
<body>
<ul id="ul1">
<li>111111</li>
<li class="box">222222</li>
<li>333333</li>
<li class="box">444444</li>
</ul>
<ol id="ol1">
<li class="box">555555</li>
<li>666666</li>
<li class="box">777777</li>
<li>888888</li>
</ol>
</body>
<head>
<meta charset="utf-8">
<title></title>
<script>
window.onload = function(){
var nodes = document.getElementsByClassName("box");
//alert(nodes.length);
for(i=0;i<nodes.length;i++){
alert(nodes[i].innerHTML);
}
}
</script>
</head>
<body>
<ul id="ul1">
<li>111111</li>
<li class="box">222222</li>
<li>333333</li>
<li class="box">444444</li>
</ul>
<ol id="ol1">
<li class="box">555555</li>
<li>666666</li>
<li class="box">777777</li>
<li>888888</li>
</ol>
</body>
<head>
<meta charset="utf-8">
<title></title>
<script>
window.onload = function(){
//var nodes = document.getElementsByClassName("box");
//// alert(nodes.length);
//for(i=0;i<nodes.length;i++){
// alert(nodes[i].innerHTML);
//}
var oUl = document.getElementById("ul1");
var aLis = oUl.getElementsByClassName("box");
for(i=0;i<aLis.length;i++){
alert(aLis[i].innerHTML);
}
}
</script>
</head>
<body>
<ul id="ul1">
<li>111111</li>
<li class="box">222222</li>
<li>333333</li>
<li class="box">444444</li>
</ul>
<ol id="ol1">
<li class="box">555555</li>
<li>666666</li>
<li class="box">777777</li>
<li>888888</li>
</ol>
</body>
<head>
<meta charset="utf-8">
<title></title>
<script>
window.onload = function(){
var oUl = document.getElementById("ul1");
var nodes = elementByClassName(oUl,"box");
for(var i=0;i<nodes.length;i++){
alert(nodes[i].innerHTML);
}
}
functionelementByClassName(parent,classStr){
var nodes = parent.getElementsByTagName("*");
var result = [];
for(vari=0;i<nodes.length;i++){
if(nodes[i].className == classStr){
result.push(nodes[i]);
}
}
return result;
}
</script>
</head>
<body>
<ul id="ul1">
<li>111111</li>
<li class="box">222222</li>
<li>333333</li>
<li class="box">444444</li>
</ul>
<ol id="ol1">
<li class="box">555555</li>
<li>666666</li>
<li class="box">777777</li>
<li>888888</li>
</ol>
</body>
赞 (0)