对Dom树的操作 appendChild ,createElement,childNodes,createTextNode
2007-02-12 10:30
573 查看
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
window.onload=function()...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//????
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
var hello = document.getElementById('hello');
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
hello.className="declared";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
var empty = document.getElementById('empty');
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
addNode(empty,"reader of","br");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
addNode(empty,"Ajax in Action","br");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
var children = empty.childNodes;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(var i=0;i<children.length;i++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
children[i].className="programe";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
empty.style.border='solid green 2px';
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
empty.style.width='200px';
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
function addNode(ele,text,br)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(text==null)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ele.appendChild(document.createElement("br"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
}else...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
var childEl =document.createElement("div");//?????
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ele.appendChild(childEl);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
var txtNode = document.createTextNode(text);//?????
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
childEl.appendChild(txtNode);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(br!=null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ele.appendChild(document.createElement(br));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
var i=0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
function addListItemUsingInnerHTML(ele,text)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
var obj = document.getElementById(ele);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
obj.innerHTML+="<div class='programe'>"+text+"</div><br>";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
function showEmpty(ele)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
var obj = document.getElementById(ele);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(obj!=null)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(obj.style.display==''||obj.style.display=='inline')...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
obj.style.display='none';
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
else...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
addNode(obj,"txt"+(i++),"br");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
obj.style.display='inline';
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
Ajax封装状态,基础
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
version:0.0.1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Date:2007.2.11 In China
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Mail:yanweiboy@hotmail.com
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
var net =new Object();//????
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
net.READY_STATE_UNINITIALIZED=0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
net.READY_STATE_LOADING=1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
net.READY_STATE_LOADED=2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
net.READY_STATE_INTERACTIVE=3;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
net.READY_STATE_COMPLETE=4;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
net.ContentLoader=function(url,onload,onerror)...{//????
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.url=url;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.req=null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.onload=onload;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.onerror=(onerror) ? onerror: this.defaultError;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.loadXMLDoc(url);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
net.ContentLoader.prototype=...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
loadXMLDoc:function(url)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(window.XMLHttpRequest)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.req=new XMLHttpRequest();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
}else if(window.ActiveXObject)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.req=new ActiveXObject("Microsoft.XMLHTTP");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(this.req)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
var loader=this;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
this.req.onreadystatechange=function()...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
loader.onReadyState.call(loader);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.req.open('GET',url,true);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.req.send(null);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
}catch(err)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.onerror.call(this);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
},
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
onReadyState:function()...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
var req = this.req;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
var ready = req.readyState;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(ready==net.READY_STATE_COMPLETE)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
var httpStatus=req.status;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(httpStatus==200||httpStatus==0)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.onload.call(this);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
}else...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.onerror.call(this);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
},
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
defaultError:function()...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
alert("error fetching data!"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
+" readyState: "+this.req.readyState
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
+" stats: "+this.req.status
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
+" headers: "+this.req.getAllResponseHeaders()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
相关文章推荐
- DOM操作(createElement()、createTextNode()、appendChild())
- 通过createElement(),createTextNode),appendChild()方法来显示文字
- js之DOM操作(创建文本节点createTextNode)
- JS中的createElement(),createTextNode(),cloneNode()
- javascript DOM innerHTML createElement createTextNode
- HTML DOM createTextNode() 方法
- createElement、createTextNode、setAttribute使用方法
- 【JS--DOM-节点操作2】--createElement()、appendChild()、insertBefore()、自定义的insertAfter()
- JS中的createElement(),createTextNode(),cloneNode()
- javascript DOM innerHTML createElement createTextNode
- javascript DOM innerHTML createElement createTextNode
- jQuery的DOM操作之设置和获取HTML、文本和值 html()text()val()
- dom操作val(),text(),html()
- createDocumentFragment提高DOM操作效率
- 谈谈innerHTML和createTextNode的区别
- js createElement appendChild createTextNode用法
- js之DOM操作访问子结点childNodes
- JavaScript中innerHTML与innerText,createTextNode的区别
- jQuery源码-dom操作之jQuery.fn.text
- js函数中document.createTextNode实现原样输出div