<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>新建网页</title>
<xml id=myXml>
<tab>
<cell name="myName1" id="Cell1">1</cell>
<cell name="myName2" id="Cell2">2</cell>
</tab>
</xml>
</head>
<body>
<input id="txt1" type="text" size="150"/>
<p>
<input type="button" value="GOGOGO"/><input type="button" onclick="add()" value="add"/>
</p><script language="JavaScript">
<!--
var HTML='';
var cel=myXml.XMLDocument.getElementsByTagName('cell');
HTML+='<table border=1><tr><td>Name</td><td>ID</td><td>Value</td></tr>';
for(i=0;i<cel.length;i++){
HTML+='<tr><td>'+cel[i].getAttribute('name')+'</td><td>'+cel[i].getAttribute('id')+'</td><td>'+cel[i].text+'</td></tr>';
}
HTML+='</table>';
document.write(HTML);function add()
{
var xmlDoc = myXml.XMLDocument;
var root = xmlDoc.documentElement
currNode = root.childNodes(0);
var newNode=xmlDoc.createElement('cell');
newNode.setAttribute('name','myName3');
newNode.setAttribute('id','Cell3');
root.appendChild(newNode);
newNode.text = "3";
txt1.value = xmlDoc.xml;
}
//-->
</script></body>
</html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>新建网页</title>
<xml id=myXml>
<tab>
<cell name="myName1" id="Cell1">1</cell>
<cell name="myName2" id="Cell2">2</cell>
</tab>
</xml>
</head>
<body>
<input id="txt1" type="text" size="150"/>
<p>
<input type="button" value="GOGOGO"/><input type="button" onclick="add()" value="add"/>
</p><script language="JavaScript">
<!--
var HTML='';
var cel=myXml.XMLDocument.getElementsByTagName('cell');
HTML+='<table border=1><tr><td>Name</td><td>ID</td><td>Value</td></tr>';
for(i=0;i<cel.length;i++){
HTML+='<tr><td>'+cel[i].getAttribute('name')+'</td><td>'+cel[i].getAttribute('id')+'</td><td>'+cel[i].text+'</td></tr>';
}
HTML+='</table>';
document.write(HTML);function add()
{
var xmlDoc = myXml.XMLDocument;
var root = xmlDoc.documentElement
currNode = root.childNodes(0);
var newNode=xmlDoc.createElement('cell');
newNode.setAttribute('name','myName3');
newNode.setAttribute('id','Cell3');
root.appendChild(newNode);
newNode.text = "3";
txt1.value = xmlDoc.xml;
}
//-->
</script></body>
</html>
document.write():
在调用 write( ) 方法时,如果该文档不处于在调用 write( ) 方法时的打开和分析的过程中,该方法将打开并清除该文档,所以它可能是有危险的。
我也想知道怎麼動態改變table
<xml id=myXml><cell name="myName1" id="Cell1">1</cell><cell name="myName2" id="Cell2">2</cell></xml>
用脚本改成
<xml id=myXml><cell name="myName2" id="Cell2">2</cell><cell name="myName1" id="Cell1">1</cell></xml>
应该怎么干?
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>新建网页</title>
<xml id=myXml><cell name="myName1" id="Cell1">1</cell><cell name="myName2" id="Cell2">2</cell></xml>
</head>
<body>
<input id="txt1" type="text" size="69"/>
<p>
<input type="button" value="GOGOGO" onclick="add()"/>
</p>
</body>
<script>
function add()
{
var sxml=myXml.innerHTML;
sxml+='<cell name="myName3" id="Cell3">3</cell>'
myXml.innerHTML=sxml;
txt1.value=myXml.innerHTML;
}
</script>
</html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>新建网页</title>
<xml id=myXml><cell name="myName1" id="Cell1">1</cell><cell name="myName2" id="Cell2">2</cell></xml>
</head>
<body>
<input id="txt1" type="text" size="69"/>
<p>
<input type="button" value="1->2" onclick="swap(1,2)"/>
<input type="button" value="2->1" onclick="swap(2,1)"/>
</p>
</body>
<script>
function swap(x,y)
{
//子节点的交换.第x个节点到第y个节点.
x=x-1;y=y-1;
var arrxml=new Array();
var sxml=myXml.innerHTML;
tmpxml=sxml.split("</cell>");
arrxml[y]=tmpxml[x]+"</cell>";
arrxml[x]=tmpxml[y]+"</cell>";
sxml=arrxml.join("");
myXml.innerHTML=sxml;
txt1.value=myXml.innerHTML;
}
</script>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>新建网页</title>
<xml id=myXml>
<tab>
<cell name="myName1" id="Cell1">1</cell>
<cell name="myName2" id="Cell2">2</cell>
</tab>
</xml>
</head>
<body onload="writeXML()">
<input id="txt1" type="text" size="150"/>
<p>
<input type="button" value="GOGOGO"/><input type="button" onclick="add()" value="add"/>
</p>
<div id="oDiv"></div>
<script language="JavaScript">
<!--
function writeXML()
{
var HTML='';
var cel=myXml.XMLDocument.getElementsByTagName('cell');
HTML+='<table border=1><tr><td>Name</td><td>ID</td><td>Value</td></tr>';
for(i=0;i<cel.length;i++){
HTML+='<tr><td>'+cel[i].getAttribute('name')+'</td><td>'+cel[i].getAttribute('id')+'</td><td>'+cel[i].text+'</td></tr>';
}
HTML+='</table>';
document.all.oDiv.innerHTML = HTML;
}
function add()
{
var xmlDoc = myXml.XMLDocument;
var root = xmlDoc.documentElement
currNode = root.childNodes(0);
var newNode=xmlDoc.createElement('cell');
newNode.setAttribute('name','myName3');
newNode.setAttribute('id','Cell3');
root.appendChild(newNode);
newNode.text = "3";
txt1.value = xmlDoc.xml;
writeXML();
}
//-->
</script></body>
</html>