楼主问的这个问题在《javascript权威指南》中作者专门讲过:Be aware, however, that you can use the write() method to output HTML to the current document only while that document is being parsed. That is, you can call document.write() from within top-level code in <script> tags only because these scripts are executed as part of the document parsing process. If you place a document.write() call within a function definition and then call that function from an event handler, it will not work as you expectin fact, it will erase the current document and the scripts it contains!
这个应该放在页面加载时执行
否则会生成新的文档 以前的都没了
//here
<script src=javascript:scriptdojs()></script>
</a></td><td>test message2</td>
//会在页面载入完成时,自动触发的
先谢谢各位了,想问下有没有别的方法输出a1的值,同时不影响后面的table内容明早结贴,再次感谢
我按照你的试了下,放在了a标签里面,可是还是不显示table的东西~~
hoho
相当与重写当前页面了
不知你想把函数中的内容输出到哪呀?
是在页面载入时,显示一个提示?还是某个块中输出! <a>中用的函数中是不能代document.write语句的,//噢我好像明白你要干什么了>?<html>
<head>
<title>111</title>
</head>
<body onclick="return dojs();">
<script language="javascript">
function dojs()
{
var a1=123;
document.write(a1);
return true;
}
</script><br><table>
<tr>
<td><a href="#">xxx</a></td><td>test message2</td>
</tr>
</table></body>
</html>
你试一试,我现在网吧没法试!我回家在我笔记本上试一试,我明天上完课,晚上再给你回复消息
你加载之后再次write会覆盖掉原来的页面内容(含table的内容)
如果要追加内容 不能适用document.write来追加。
请用appendChild或者innerHTML innerText 这些来操作
document.write();
document.close();
function mohu(){
document.open();
document.write("sdfsdf");
document.write("ssssssssssssssssss");
document.close();
}调用此方法后页面输出:"sdfsdfssssssssssssssssss"
<body onclick="dojs();">
<script language="javascript">
function dojs()
{
var a1="1234";
// document.open();
document.write(a1);
// document.close();
}
</script>
<br>
<script language="javascript">onload=document.write("^__^");</script>
<table>
<tr>
<td><a href="#">xxx</a></td><td>test message2</td>
</tr>
</table>
</body>
</html>