<html><head>
<script type="text/javascript">
function reWriteBody(){
var tagList = document.getElementsByTagName("td");
for(var i=0;i<tagList.length;i++){
window.document.write(tagList[i].nodeName);
}
}
</script>
<title>Hello JavaScript</title>
</head>
<body>
<table>
<tr>
<td id="td1">data1</td>
<td id="td2">data2</td>
<td id="td3">data3</td>
</tr>
</table><input type="button" value="Click Here" onclick="reWriteBody()">
</body></html>为什么其中的JS在运行的时候报"没有权限"的错误?
<script type="text/javascript">
function reWriteBody(){
var tagList = document.getElementsByTagName("td");
for(var i=0;i<tagList.length;i++){
window.document.write(tagList[i].nodeName);
}
}
</script>
<title>Hello JavaScript</title>
</head>
<body>
<table>
<tr>
<td id="td1">data1</td>
<td id="td2">data2</td>
<td id="td3">data3</td>
</tr>
</table><input type="button" value="Click Here" onclick="reWriteBody()">
</body></html>为什么其中的JS在运行的时候报"没有权限"的错误?
<script type="text/javascript" defer>
function reWriteBody(){
var tagList = document.getElementsByTagName("td");
var s ="";
for(var i=0;i<tagList.length;i++){
s+=tagList[i].nodeName;
}
document.write(s);
document.close();
}
</script>
<title>Hello JavaScript</title>
</head>
<body>
<table>
<tr>
<td id="td1">data1</td>
<td id="td2">data2</td>
<td id="td3">data3</td>
</tr>
</table><input type="button" value="Click Here" onclick="reWriteBody()">
</body></html>
“在载人页面后,浏览器输出流自动关闭;在此之后,任何一个对当前页面进行操作的document.write()方法将打开—个新的输出流。它将清除当前页面内容(包括源文档的任何变量或值)、因此.假如希望用脚本生成的HTML替换当前页面,就必须把HTML内容连接起来赋给一个变量、使用一个 document.write()方法完成写操作,不必清除文档并打开一个新数据流,一个document.write()调用就可完成所有的操作。 ”
<script type="text/javascript">
function reWriteBody(){
document.write("Line 1");//Line 1
document.write("Line 2");//Line 2
}
</script>
<title>Hello JavaScript</title>
</head>
<body>
Hello JavaScript
<table>
<tr>
<td id="td1">data1</td>
<td id="td2">data2</td>
<td id="td3">data3</td>
</tr>
</table><input type="button" value="Click Here" onclick="reWriteBody()">
</body></html>
我看好像按照你的意思,这样写的话:
document.write("Line 1");//Line 1
document.write("Line 2");//Line 2
是打开了两个输出流,结果应该是页面显示"Line1"后立即被全部刷掉只显示"Line 2"
但是实际结果是Line1 和 Line2都显示.只有当这么写的时候:
document.write("Line 1");//Line 1
document.close();
document.write("Line 2");//Line 2
才会只显示Line2
这是怎么回事呢?