DOM
DOM中的三个字母,D(文档)可以理解为整个Web加载的网页文档;O(对象)可以理解为类似window对象之类的东西,可以调用属性和方法,这里我们说的是document对象;M(模型)可以理解为网页文档的树型结构
节点
节点种类
查找元素
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div style="color:black" id="box" title="mytitle" class="class" bbb="bbb_value">
hello
</div>
<input id="input" type="text" value="123"/>
<input id="input1" type="radio" checked/>
</body>
<script type="text/javascript">
document.getElementById('box').id;//获取id
//document.getElementById('box').id = 'person';//设置id
document.getElementById('box').title;//获取title
document.getElementById('box').title = 'new_title';//设置title
document.getElementById('box').style;//获取CSSStyleDeclaration对象
document.getElementById('box').style.color;//获取style对象中color的值
document.getElementById('box').style.color = 'red';//设置style对象中color的值
document.getElementById('box').className;//获取class
document.getElementById('box').className = 'box';//设置class
document.getElementById('box').getAttribute('className');//非IE不支持
console.log(document.getElementById('box').bbb);//获取自定义属性的值,非IE不支持
document.getElementById('box').setAttribute("bbb","new_bbb_value");
console.log(document.getElementById('box').getAttribute("bbb"));//new__bbb_value
document.getElementById('box').removeAttribute("bbb");//删除属性
console.log(document.getElementById('box').getAttribute("bbb"));//null
console.log(document.getElementById('input').value);//1234
console.log(document.getElementById('input1').checked);//true
</script>
</html>
DOM节点
node节点属性
<script type="text/javascript">
console.log(document.getElementById('box').nodeType);//1,元素节点
console.log(document.getElementById('box').nodeName);//Div
console.log(document.getElementById('box').getAttribute("style").nodeValue);//undefined,getAttribute获取的是值,不是属性节点
console.log(document.getElementById('box').attributes.item(0).nodeValue);//color:black
</script>
层次节点属性
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div style="color:black" id="box" title="mytitle" class="class" bbb="bbb_value">
<span>hello1</span>
<span>hello2</span>
<span>hello3</span>
</div>
</body>
<script type="text/javascript">
var div = document.getElementById('box')
console.log(div.innerHTML);
/*
<span>hello1</span>
<span>hello2</span>
<span>hello3</span>
*/
console.log(div.childNodes.length);//得到子节点个数,IE3个,非IE7个,换行会产生空白节点
console.log(div.childNodes[0].nodeValue);//输出空白
console.log(div.attributes['bbb'].nodeValue);//bbb_value
console.log(div.attributes.getNamedItem('bbb').nodeValue);//和上面效果一样
console.log(div.firstChild.nodeValue);//输出空白
console.log(div.firstChild.innerHTML);//undefined
console.log(div.lastChild.nodeValue);//输出空白
console.log(div.ownerDocument);//#document
console.log(div.childNodes[0].nextSibling.innerHTML);//hello1
console.log(div.childNodes[2].previousSibling.innerHTML);//hello2
console.log(div.parentNode);//body对象
</script>
</html>
节点操作
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
</body>
<script type="text/javascript">
document.write("<div id='box'></div>");
var span = document.createElement("span");
var text = document.createTextNode("hello");
span.appendChild(text);
document.getElementById("box").appendChild(span);
var h = document.createElement("h1");
var text1 = document.createTextNode("h1");
h.appendChild(text1);
document.getElementById("box").insertBefore(h,span);
var input = null;
input = document.createElement('input');
input.setAttribute('type', 'radio');
input.setAttribute('name', 'sex');
document.getElementById("box").appendChild(input);
//替换节点
var text2 = document.createTextNode("new_hello");
span.replaceChild(text2,span.childNodes[0]);
//克隆节点
var span1 = span.cloneNode(true);//true会复制内容,否则只复制结构
span1.id = "span1";
document.getElementById("box").appendChild(span1);
//删除节点
document.getElementById("box").removeChild(document.getElementById("span1"));
</script>
</html>