标签:attribute view ref att ntb asc box utf-8 doctype
一、DOM节点
1.获取子节点:
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> <script> window.onload=function () { var oUl=document.getElementById(‘ul1‘); //IE6-8 //alert(oUl.childNodes.length); for(var i=0;i<oUl.childNodes.length;i++) { //nodeType==3 -> 文本节点 //nodeType==1 -> 元素节点 //alert(oUl.childNodes[i].nodeType); if(oUl.childNodes[i].nodeType==1) { oUl.childNodes[i].style.background=‘red‘; } } }; </script> </head> <body> <ul id="ul1"> <li></li> <li></li> </ul> aaaa bbbb fafafsdfasd 文本节点 <span>qwerqwre 元素节点</span> </body> </html>
当获取ul1下面的childNodes时,chrome和FF下length都是5,因为把空的文本节点也算上了。
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> <script> window.onload=function () { var oUl=document.getElementById(‘ul1‘); //alert(oUl.children.length); for(var i=0;i<oUl.children.length;i++) { oUl.children[i].style.background=‘red‘; } }; </script> </head> <body> <ul id="ul1"> <li></li> <li></li> </ul> aaaa bbbb fafafsdfasd 文本节点 <span>qwerqwre 元素节点</span> </body> </html>
使用children获取到的length就是2了。
2.parentNode
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> <script> window.onload=function () { var aA=document.getElementsByTagName(‘a‘); for(var i=0;i<aA.length;i++) { aA[i].onclick=function () { this.parentNode.style.display=‘none‘; }; } }; </script> </head> <body> <ul id="ul1"> <li>dfasdf <a href="javascript:;">隐藏</a></li> <li>45346 <a href="javascript:;">隐藏</a></li> <li>fghfgcvn <a href="javascript:;">隐藏</a></li> <li>vcbxcvbc <a href="javascript:;">隐藏</a></li> <li>757465867 <a href="javascript:;">隐藏</a></li> </ul> </body> </html>
3.offsetParent
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> <style> #div1 {width:200px; height:200px; background:#CCC; margin:100px; } #div2 {width:100px; height:100px; background:red; position:absolute; left:50px; top:50px;} </style> <script> window.onload=function () { var oDiv2=document.getElementById(‘div2‘); alert(oDiv2.offsetParent); }; </script> </head> <body> <div id="div1"> <div id="div2"></div> </div> </body> </html>
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> <script> window.onload=function () { var oUl=document.getElementById(‘ul1‘); //IE6-8 //oUl.firstChild.style.background=‘red‘; //高级浏览器 //oUl.firstElementChild.style.background=‘red‘; if(oUl.firstElementChild) { oUl.firstElementChild.style.background=‘red‘; } else { oUl.firstChild.style.background=‘red‘; } }; </script> </head> <body> <ul id="ul1"> <li>1</li> <li>2</li> <li>3</li> </ul> </body> </html>
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> <script> window.onload=function () { var oTxt=document.getElementById(‘txt1‘); var oBtn=document.getElementById(‘btn1‘); oBtn.onclick=function () { //oTxt.value=‘asdfasd‘; //oTxt[‘value‘]=‘xczcvb‘; oTxt.setAttribute(‘value‘, ‘erwertwert‘); }; }; </script> </head> <body> <input id="txt1" type="text" /> <input id="btn1" type="button" value="按钮" /> </body> </html>
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> <script> function getByClass(oParent, sClass) { var aResult=[]; var aEle=oParent.getElementsByTagName(‘*‘); for(var i=0;i<aEle.length;i++) { if(aEle[i].className==sClass) { aResult.push(aEle[i]); } } return aResult; } window.onload=function () { var oUl=document.getElementById(‘ul1‘); var aBox=getByClass(oUl, ‘box‘); for(var i=0;i<aBox.length;i++) { aBox[i].style.background=‘red‘; } }; </script> </head> <body> <ul id="ul1"> <li class="box"></li> <li class="box"></li> <li></li> <li></li> <li></li> <li class="box"></li> <li></li> </ul> </body> </html>
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> <script> window.onload=function () { var oBtn=document.getElementById(‘btn1‘); var oUl=document.getElementById(‘ul1‘); var oTxt=document.getElementById(‘txt1‘); oBtn.onclick=function () { var oLi=document.createElement(‘li‘); var aLi=oUl.getElementsByTagName(‘li‘); oLi.innerHTML=oTxt.value; //父级.appendChild(子节点); //oUl.appendChild(oLi); if(aLi.length>0) { oUl.insertBefore(oLi, aLi[0]); } else { oUl.appendChild(oLi); } }; }; </script> </head> <body> <input id="txt1" type="text"/> <input id="btn1" type="button" value="创建li"/> <ul id="ul1"> </ul> </body> </html>
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> <script> window.onload=function () { var aA=document.getElementsByTagName(‘a‘); var oUl=document.getElementById(‘ul1‘); for(var i=0;i<aA.length;i++) { aA[i].onclick=function () { oUl.removeChild(this.parentNode); }; } }; </script> </head> <body> <ul id="ul1"> <li>asfasd <a href="javascript:;">删除</a></li> <li>5645 <a href="javascript:;">删除</a></li> <li>ghdfjgj <a href="javascript:;">删除</a></li> <li>mvbnmvnb <a href="javascript:;">删除</a></li> </ul> </body> </html>
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> <script> window.onload=function () { var oUl=document.getElementById(‘ul1‘); var oFrag=document.createDocumentFragment(); for(var i=0;i<10000;i++) { var oLi=document.createElement(‘li‘); //oUl.appendChild(oLi); oFrag.appendChild(oLi); } oUl.appendChild(oFrag); }; </script> </head> <body> <ul id="ul1"> </ul> </body> </html>
标签:attribute view ref att ntb asc box utf-8 doctype
原文地址:http://www.cnblogs.com/tenWood/p/7670174.html