<html>
<head>
<title> Untitled Document </title>
<script type ="text/javascript">
// 同样的两次调用document.write(),为什么一会发生错误,一个不会?
function learn_write_wrong(){
var pre = document.getElementsByTagName("pre");
document.write(pre[0].innerHTML, "<br />");
document.write(pre[0].id, "<br />"); // 这句话发生了错误! why?
}
function learn_write_right(){
document.write("right", "<br />");
document.write("right yeah!", "<br />");
}
</script>
</head>
<body>
<pre id = "实验document.write()方法">
网页原内容
</pre>
<input type = "button" value = "错误" onclick = "javascript: learn_write_wrong();" />
<input type = "button" value = "正确" onclick = "javascript: learn_write_right();" />
</body>
</html>
代码和问题都在里面了!望csdn的大虾们指点迷津啊!!!!!!!
------解决方案--------------------
你把document.write(pre[0].innerHTML, "<br />");注释掉document.write(pre[0].id, "<br />"); 就可以正常运行了 原因可能是这样的 因为是页面已经加载完毕了 当你点错误按钮后 先运行document.write(pre[0].innerHTML, "<br />");,此时整个页面都已被document.write(pre[0].innerHTML, "<br />");,覆盖掉了 也就是说此时的pre[0]已经不存在了 所以没法输出pre[0].id 但如果这样的话应该会报错才对 貌似没有呢 不是很清楚了 帮你顶下吧
------解决方案--------------------
http://hi.baidu.com/lmcbbat/blog/item/5d40c473fb3a19138601b0c8.html你参考这个看看