当前位置: 代码迷 >> JavaScript >> appendChild你如何了?
  详细解决方案

appendChild你如何了?

热度:28   发布时间:2012-09-21 15:47:26.0
appendChild你怎么了??
ie9 f12 返回的结果SCRIPT5007: Unable to get value of the property 'appendChild': object is null or undefined
<!DOCTYPE html>
<html>
<head><title></title><script type="text/javascript">
var para=document.createElement("p");
var test=document.getElementById("test");
var text=document.createTextNode('hello word!');
para.appendChild(text); 
test.appendChild(para);
</script></head>
<body>
<div id="test"></div>
</body>
</html>
ie9 firefox 都不能appendchild??那里错了?求解答

------解决方案--------------------
<!DOCTYPE html>
<html>
<head><title></title><script type="text/javascript">
window.onload=function(){
var para=document.createElement("p");
var test=document.getElementById("test");
var text=document.createTextNode('hello word!');
para.appendChild(text); 
test.appendChild(para);
}
</script></head>
<body>
<div id="test"></div>
</body>
</html>