DOM
: document object model(文档对象模型)
节点类型:
元素节点 <div></div>
属性节点 id = 'div1'
文本节点 div文本
元素节点的获取
document.getElementById(id)
功能:通过id获取符合条件的元素(id必须是唯一的)
返回值:就是符合条件的一个节点
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
window.onload = function(){
//写在这里的代码,是整个页面加载完成以后执行
var oDiv = document.getElementById("div1");
/*
获取行间属性的值
*/
alert(oDiv.id);
alert(oDiv.title);
//访问class 通过className访问
alert(oDiv.className);
//设置行间属性的值
oDiv.id = "div2";
oDiv.title = 'world';
oDiv.className = 'box4';
/*
只能访问标签行间的css样式
*/
alert(oDiv.style.width);
alert(oDiv.style.height);
/*
[注] 如果CSS样式带一,将一去掉,从第二个单词开始首字母大写
*/
oDiv.style.backgroundColor = 'blue';
alert(oDiv.style.backgroundColor);
}
</script>
</head>
<body>
<div id = 'div1' title="hello" class="box" style="width: 300px;height: 300px; background-color: red;">div文本</div>
</body>
</html>
获取元素节点
document.getElementById(id);
node.getElementsByTagName(标签名);
功能:通过标签名获取符合条件的元素节点
返回值:伪数组/类数组
node.getElementsByClassName(class名字)
功能:通过class名字获取符合条件的元素节点。
document.getElementsByName(name属性的值)
功能:通过name属性的值获取符合条件的元素节点。
(1)
<script>
window.onload = function(){
var aLis = document.getElementsByTagName("li");
//使用起来和数组类似,一般情况,把这种叫做伪数组/类数组(自己起)
alert(aLis);
alert(aLis.length);
}
</script>
<body>
<ul>
<li>111</li>
<li class="box">111</li>
<li>111</li>
<li>111</li>
<li class="box">111</li>
<li name = 'hello'>111</li>
<div class="box" name = 'hello'></div>
</ul>
<input type="text" name="hello">
<span name = 'hello'></span>
<ol>
<li>222</li>
<li class="box">222</li>
<li class="box">222</li>
<li>222</li>
</ol>
</body>
(2) node.getElementsByTagName
(标签名);
<script>
window.onload = function(){
var a = document.getElementById("ol1");
var aLis = a.getElementsByTagName("li");
//获取ol下的li节点
alert(aLis.length);
for(var i = 0; i < aLis.length; i++){
aLis[i].style.backgroundColor = "red";
}
}
}
</script>
<body>
<ul>
<li>111</li>
<li class="box">111</li>
<li>111</li>
<li>111</li>
<li class="box">111</li>
<li name = 'hello'>111</li>
<div class="box" name = 'hello'></div>
</ul>
<input type="text" name="hello">
<span name = 'hello'></span>
<ol id="ol1">
<li>222</li>
<li class="box">222</li>
<li class="box">222</li>
<li>222</li>
</ol>
</body>
(3) node.getElementsByClassName
(class名字)
<script>
window.onload = function(){
var a = document.getElementById("ul1");
var nodes = a.getElementsByClassName("box");
alert(nodes.length);
for(var i = 0; i < nodes.length; i++){
nodes[i].style.backgroundColor = "red";
}
}
</script>
<body>
<ul id="ul1">
<li>111</li>
<li class="box">111</li>
<li>111</li>
<li>111</li>
<li class="box">111</li>
<li name = 'hello'>111</li>
<div class="box" name = 'hello'></div>
</ul>
<input type="text" name="hello">
<span name = 'hello'></span>
<ol id="ol1">
<li>222</li>
<li class="box">222</li>
<li class="box">222</li>
<li>222</li>
</ol>
</body>
(4) document.getElementsByName
(name属性的值)
<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>
<body>
<ul id="ul1">
<li>111</li>
<li class="box">111</li>
<li>111</li>
<li>111</li>
<li class="box">111</li>
<li name = 'hello'>111</li>
<div class="box" name = 'hello'></div>
</ul>
<input type="text" name="hello">
<span name = 'hello'></span>
<ol id="ol1">
<li>222</li>
<li class="box">222</li>
<li class="box">222</li>
<li>222</li>
</ol>
</body>
document.querySelector()
返回值:一个元素节点,找到符合条件的第一个元素节点
document.querySelectorAll()
返回值:返回值,是一个伪数组
参数:字符串 CSS选择器格式字符串
<script>
window.onload = function(){
//var nodes = document.querySelectorAll('#ul1')
var nodes = document.querySelectorAll('ol li')
// var nodes = document.querySelectorAll('ol .box')
// var nodes = document.querySelectorAll('ul [name=hello');
alert(nodes.length);
for(var i = 0; i < nodes.length; i++){
nodes[i].style.backgroundColor = "red";
}
}
</script>
<body>
<ul id="ul1">
<li>111</li>
<li class="box">111</li>
<li>111</li>
<li>111</li>
<li class="box">111</li>
<li name = 'hello'>111</li>
<div class="box" name = 'hello'></div>
</ul>
<input type="text" name="hello">
<span name = 'hello'></span>
<ol id="ol1">
<li>222</li>
<li class="box">222</li>
<li class="box">222</li>
<li>222</li>
</ol>
</body>
自定义byClassName方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
function elementsByClassName(node,classStr){
//获取node这个节点下所有的子节点
var nodes = node.getElementsByTagName("*");
var arr = [];//存放符合条件的节点
for(var i = 0; i < nodes.length; i++){
if(nodes[i].className == classStr){
arr.push(nodes[i]);
}
}
return arr;
}
window.onload = function(){
var a = document.getElementById("ol1");
var nodes = elementsByClassName(a,"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>111</li>
<li>111</li>
<li class="box">111</li>
<li name = 'hello'>111</li>
<div class="box" name = 'hello'></div>
</ul>
<input type="text" name="hello">
<span name = 'hello'></span>
<ol id="ol1">
<li>222</li>
<li class="box">222</li>
<li class="box">222</li>
<li>222</li>
</ol>
</body>
</html>