JS获取元素节点的方法
document.getElementById(id);
node.getElementsByTagName(标签名);
功能:从node节点开始,通过标签名获取符合条件的元素节点
返回值:伪数组/类数组
node.getElementsByClassName(class节点) (IE8一下不兼容)
功能:通过class名字获取符合条件的元素节点
document.getElementsByName(name属性的值)
功能:通过Name属性的值获取符合条件的元素节点
【注】一般使用在表单元素里
document.querySelector()
返回值:一个元素节点,找到符合条件的第一个元素节点
document.querySelectorAll()
返回值:返回值是一个伪数组
参数:字符串 CSS选择器格式字符串
例子一:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
window.onload = function(){
var aLis = document.getElementsByTagName("li");
alert(aLis);//使用起来和数组类似,一般情况,把这种叫做伪数组/类数组
alert(aLis.length);
}
</script>
</head>
<body>
<ul id = 'ul1'>
<li>111</li>
<li class='box'>111</li>
<li class='box'>111</li>
<li name='hello'>111</li>
<li>111</li>
<li>111</li>
<div class='box' name='hello'>111</div>
</ul>
<input type="text" name='hello'/>
<br>
<span name='hello'>111</span>
<ol id = 'ol1'>
<li class='box'>111</li>
<li class='box'>111</li>
<li>111</li>
<li>111</li>
<li>111</li>
<li>111</li>
</ol>
</body>
</html>
运行结果:
例子二:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
window.onload = function(){
var oOl = document.getElementById("ol1");
var aLis = oOl.getElementsByTagName("li");
//获取ol下的li节点
alert(aLis.length);
for(var i=0;i<aLis.length;i++)
{
aLis[i].style.backgroundColor = 'red';
}
}
</script>
</head>
<body>
<ul id = 'ul1'>
<li>111</li>
<li class='box'>111</li>
<li class='box'>111</li>
<li name='hello'>111</li>
<li>111</li>
<li>111</li>
<div class='box' name='hello'>111</div>
</ul>
<input type="text" name='hello'/>
<br>
<span name='hello'>111</span>
<ol id = 'ol1'>
<li class='box'>111</li>
<li class='box'>111</li>
<li>111</li>
<li>111</li>
<li>111</li>
<li>111</li>
</ol>
</body>
</html>
运行结果:
例子三:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
window.onload = function(){
var nodes = document.getElementsByClassName("box");
alert(nodes.length);
for(var i=0;i<nodes.length;i++)
{
nodes[i].style.backgroundColor = 'red';
}
}
</script>
</head>
<body>
<ul id = 'ul1'>
<li>111</li>
<li class='box'>111</li>
<li class='box'>111</li>
<li name='hello'>111</li>
<li>111</li>
<li>111</li>
<div class='box' name='hello'>111</div>
</ul>
<input type="text" name='hello'/>
<br>
<span name='hello'>111</span>
<ol id = 'ol1'>
<li class='box'>111</li>
<li class='box'>111</li>
<li>111</li>
<li>111</li>
<li>111</li>
<li>111</li>
</ol>
</body>
</html>
运行结果:
例子四:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
window.onload = function(){
var oUl = document.getElementById("ul1");
var nodes = oUl.getElementsByClassName("box");
alert(nodes.length);
for(var i=0;i<nodes.length;i++)
{
nodes[i].style.backgroundColor = 'red';
}
}
</script>
</head>
<body>
<ul id = 'ul1'>
<li>111</li>
<li class='box'>111</li>
<li class='box'>111</li>
<li name='hello'>111</li>
<li>111</li>
<li>111</li>
<div class='box' name='hello'>111</div>
</ul>
<input type="text" name='hello'/>
<br>
<span name='hello'>111</span>
<ol id = 'ol1'>
<li class='box'>111</li>
<li class='box'>111</li>
<li>111</li>
<li>111</li>
<li>111</li>
<li>111</li>
</ol>
</body>
</html>
运行结果:
例子五:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
window.onload = function(){
var nodes = document.getElementsByName("hello");
alert(nodes.length);
for(var i=0;i<nodes.length;i++)
{
nodes[i].style.backgroundColor = 'red';
}
}
</script>
</head>
<body>
<ul id = 'ul1'>
<li>111</li>
<li class='box'>111</li>
<li class='box'>111</li>
<li name='hello'>111</li>
<li>111</li>
<li>111</li>
<div class='box' name='hello'>111</div>
</ul>
<input type="text" name='hello'/>
<br>
<span name='hello'>111</span>
<ol id = 'ol1'>
<li class='box'>111</li>
<li class='box'>111</li>
<li>111</li>
<li>111</li>
<li>111</li>
<li>111</li>
</ol>
</body>
</html>
运行结果:
例子六:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
window.onload = function(){
var nodes = document.querySelectorAll("#ul1");
alert(nodes.length);
for(var i=0;i<nodes.length;i++)
{
nodes[i].style.backgroundColor = 'red';
}
}
</script>
</head>
<body>
<ul id = 'ul1'>
<li>111</li>
<li class='box'>111</li>
<li class='box'>111</li>
<li name='hello'>111</li>
<li>111</li>
<li>111</li>
<div class='box' name='hello'>111</div>
</ul>
<input type="text" name='hello'/>
<br>
<span name='hello'>111</span>
<ol id = 'ol1'>
<li class='box'>111</li>
<li class='box'>111</li>
<li>111</li>
<li>111</li>
<li>111</li>
<li>111</li>
</ol>
</body>
</html>
运行结果:
例子七:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
window.onload = function(){
var nodes = document.querySelectorAll("li");
alert(nodes.length);
for(var i=0;i<nodes.length;i++)
{
nodes[i].style.backgroundColor = 'red';
}
}
</script>
</head>
<body>
<ul id = 'ul1'>
<li>111</li>
<li class='box'>111</li>
<li class='box'>111</li>
<li name='hello'>111</li>
<li>111</li>
<li>111</li>
<div class='box' name='hello'>111</div>
</ul>
<input type="text" name='hello'/>
<br>
<span name='hello'>111</span>
<ol id = 'ol1'>
<li class='box'>111</li>
<li class='box'>111</li>
<li>111</li>
<li>111</li>
<li>111</li>
<li>111</li>
</ol>
</body>
</html>
运行结果:
例子八:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
window.onload = function(){
var nodes = document.querySelectorAll("ol .box");
alert(nodes.length);
for(var i=0;i<nodes.length;i++)
{
nodes[i].style.backgroundColor = 'red';
}
}
</script>
</head>
<body>
<ul id = 'ul1'>
<li>111</li>
<li class='box'>111</li>
<li class='box'>111</li>
<li name='hello'>111</li>
<li>111</li>
<li>111</li>
<div class='box' name='hello'>111</div>
</ul>
<input type="text" name='hello'/>
<br>
<span name='hello'>111</span>
<ol id = 'ol1'>
<li class='box'>111</li>
<li class='box'>111</li>
<li>111</li>
<li>111</li>
<li>111</li>
<li>111</li>
</ol>
</body>
</html>
运行结果:
例子九:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
window.onload = function(){
var nodes = document.querySelectorAll("[name=hello]");
alert(nodes.length);
for(var i=0;i<nodes.length;i++)
{
nodes[i].style.backgroundColor = 'red';
}
}
</script>
</head>
<body>
<ul id = 'ul1'>
<li>111</li>
<li class='box'>111</li>
<li class='box'>111</li>
<li name='hello'>111</li>
<li>111</li>
<li>111</li>
<div class='box' name='hello'>111</div>
</ul>
<input type="text" name='hello'/>
<br>
<span name='hello'>111</span>
<ol id = 'ol1'>
<li class='box'>111</li>
<li class='box'>111</li>
<li>111</li>
<li>111</li>
<li>111</li>
<li>111</li>
</ol>
</body>
</html>
运行结果: