日期:2014-05-16  浏览次数:20568 次

为什么新加的节点显示不出来
代码贴出来:

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
  var element = document.createElement("div");
  element.className("mod");
  var txtNode = document.createTextNode("hello world!");
  element.appendChild(txtNode);
  document.body.appendChild(element);
</script>
</head>
<body>
<ul id="list">
  <li> 123 </li>
  <li>456</li>
  <li>789</li>
</ul>
</body>
</html>
按说新添加的节点应该出来的吧,可是出不来,入门新手,请各位指点。

------解决方案--------------------
HTML code

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<ul id="list">
  <li> 123 </li>
  <li>456</li>
  <li>789</li>
</ul>
<script type="text/javascript">
  var element = document.createElement("div");
  element.className="mod";
  var txtNode = document.createTextNode("hello world!");
  element.appendChild(txtNode);
  document.body.appendChild(element);
</script>
</body>
</html>

------解决方案--------------------
HTML code
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
window.onload=function(){
  var element = document.createElement("div");
  element.className="mod";
  var txtNode = document.createTextNode("hello world!");
  element.appendChild(txtNode);
  document.body.appendChild(element);
 } 
</script>
</head>
<body>
<ul id="list">
  <li> 123 </li>
  <li>456</li>
  <li>789</li>
</ul>
</body>
</html>

------解决方案--------------------
HTML code
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>

</head>
<body>
<ul id="list">
  <li> 123 </li>
  <li>456</li>
  <li>789</li>
</ul>
<script type="text/javascript">
//由于需要添加将节点添加到body中,所以代码必须写在body标签之后,否则就办法获取到body元素
//或者也可以将代码写到window.onload 事件处理函数中
  var element = document.createElement("div");
  element.className = 'mod'; //className是属性,不是方法
  var txtNode = document.createTextNode("hello world!");
  element.appendChild(txtNode);
  document.body.appendChild(element);
</script>
</body>
</html>