js动态的利用appendChild添加元素时报错。
ff下为:Node cannot be inserted at the specified point in the hierarchy" code: "3
ie下为:不支持此接口
用的库为YUI3.
ff下为:Node cannot be inserted at the specified point in the hierarchy" code: "3
ie下为:不支持此接口
用的库为YUI3.
<SCRIPT>
function fnAppend(){
var oNewNode = document.createElement("LI");
oList.appendChild(oNewNode);
oNewNode.innerHTML="List node 5";
}
</SCRIPT>
<BODY>
<UL ID = oList>
<LI>List node 1
<LI>List node 2
<LI>List node 3
<LI>List node 4
</UL>
<INPUT TYPE = "button" VALUE = "Append Child" onclick = "fnAppend()">
</BODY>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> new document </title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css">
#ajaxload{margin:0 auto;border:1px solid #F1F1F1;background-color:#F9F9F9; font-family:verdana; width:600px;height:30px;line-height:30px;text-align:center;}
</style>
</head> <body>
<b>TEST</b>
<hr size="1" color="#FF3300">
<p> </p>
<ul id="ul01">
<li>Item01 </li>
<li>Item02 </li>
<li>Item03 </li>
<li>Item04 </li>
<li>Item05 </li>
<li>Item06 </li>
<li>Item07 </li>
<li>Item08 </li>
</ul> <p> JS 增加 动态增加元素</p>
<input name="btn01" value="btn01" type="button" onClick="javascript:addLi();" /><br />
<script language="javascript" type="text/javascript">
var i=9;
function addLi()
{
var t=document.createTextNode("Item:0"+(i++));
var n=document.createElement('LI');
n.appendChild(t);
var p=document.getElementById("ul01");
p.appendChild(n);
}
</script></body>
</html>
var d = document.createElement("div");
d.setAttribute("width", 200);
document.body.appendChild(d);
oNewNode.innerHTML="List node 5";innerHTML虽然是跨浏览器属性 但没有纳入规范
要用appendChild 必须使用createTextNode方法创建文本才行oList.appendChild(document.createTextNode("List node 5"));