元素的创建、删除、插入、替换
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>DOM</title>
</head>
<script>
window.onload=function (){
var aInput=document.getElementsByTagName('input');
var oUl=document.getElementsByTagName('ul')[0];
var oP=document.getElementsByTagName('p')[0];
var oDiv=document.getElementsByTagName('div')[0];
aInput[1].onclick=function (){
var oLi=document.createElement('li');
oLi.innerHTML=aInput[0].value;
var oA=document.createElement('a');
oA.innerHTML='删除';
oA.href='javascript:;';
if (oUl.children[0]) {
oUl.insertBefore(oLi,oUl.children[0]);
oLi.appendChild(oA);
}else{
oUl.appendChild(oLi);
oLi.appendChild(oA);
}
oA.onclick=function (){
oUl.removeChild(this.parentNode);
}
}
aInput[2].onclick=function (){
document.body.replaceChild(oDiv, oP);
}
};
</script>
<body>
<input type="text" name="">
<input type="button" value="添加" name="">
<br>
<p>p标签:22222222</p>
<input type="button" value="替换" name="">
<div> div标签:1111111111</div>
<ul></ul>
</body>
</html>
元素的宽高
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>DOM</title>
<style type="text/css">
div{width: 100px;height: 100px;border:1px solid #000;padding: 10px;}
</style>
</head>
<body>
<div id="div1"></div>
<script>
var oDiv1=document.getElementById('div1');
alert(getComputedStyle(oDiv1).width);
alert(oDiv1.clientWidth);
alert(oDiv1.offsetWidth);
</script>
</body>
</html>