web板块那边,发了没人回复,所以发这里来了.<html>
<head>
<title>我的网页</title>
</head>
<body text="navy">
<p>留得青山在,不怕没柴烧
<form id="f001" name="ff">
<input type=button value="click me" name="b1" onClick=tt();>
<br><br><br>
<select name="slt" size=5 multiple>
<option value=10>奔驰
<option value=21 >宝马
<option value=3>三菱
<option value=4>法拉利
<option value=5>宾利
<option value=6>丰田
<option value=7>别克
</select>
</form>
<hr>
<script language="JavaScript" >
<!--
function tt(){
var op=document.ff.slt.options;
//document.write(op.length+"<br>"); //(1)
for(var i=0 ; i<op.length; i++){
document.write("###"+"<br>"); //字符串"###"
}
}
-->
</script>
<hr>
<p>驿外断桥边,寂寞开无主
</body> </html>预期结果:
按下按钮"click me"后,字符串"###"被打印7遍。实际结果:
只打印了1遍。而且,如果放开语句(1)的注释(也就是执行语句(1)),
则字符串"###"一遍都不打印,
这是为什么啊???
<head>
<title>我的网页</title>
</head>
<body text="navy">
<p>留得青山在,不怕没柴烧
<form id="f001" name="ff">
<input type=button value="click me" name="b1" onClick=tt();>
<br><br><br>
<select name="slt" size=5 multiple>
<option value=10>奔驰
<option value=21 >宝马
<option value=3>三菱
<option value=4>法拉利
<option value=5>宾利
<option value=6>丰田
<option value=7>别克
</select>
</form>
<hr>
<script language="JavaScript" >
<!--
function tt(){
var op=document.ff.slt.options;
//document.write(op.length+"<br>"); //(1)
for(var i=0 ; i<op.length; i++){
document.write("###"+"<br>"); //字符串"###"
}
}
-->
</script>
<hr>
<p>驿外断桥边,寂寞开无主
</body> </html>预期结果:
按下按钮"click me"后,字符串"###"被打印7遍。实际结果:
只打印了1遍。而且,如果放开语句(1)的注释(也就是执行语句(1)),
则字符串"###"一遍都不打印,
这是为什么啊???
改成: for(var i=0 ; i<op.length; i++){
//document.write("###"+"<br>"); //字符串"###"
alert("###");
}
你就会看到alert7次了!
当前document已经有内容了,你不能直接用document.write向文档添加内容,可以用DOM来做。
function tt(){
var op = document.ff.slt.options;
//document.write(op.length+"<br>"); //(1)
var str = "";
for(var i=0 ; i< op.length; i++){
str+="####<br>"; //字符串"###"
}
document.write(str);
You should not use the write or writeln methods on the current document after the document has finished loading unless you first call the open method, which clears the current document's window and erases all variables.
In general, it is not necessary to open the document using the document.open method, since the document.write method will automatically open the file and discard (erase) the contents. However, after the write is complete, you need to close the document by using the document.close method. In some browsers, the results of the write may not be completely displayed, due to buffering, until the close occurs.
<!--
function tt(){
var op=document.ff.slt.length;
//document.write(op.length+"<br>"); //(1)
for(var i=0 ; i<op; i++){
document.writeln("###"+"<br>"); //字符串"###"
}
}
-->
</script>
有道理,但是为什么,这样又可以呢?
把循环条件i<op.length改为i<7,
改成常量,又可以执行7此write()方法了
<html>
<head>
<title>我的网页</title>
</head>
<body text="navy">
<p>留得青山在,不怕没柴烧
<form id="f001" name="ff">
<input type=button value="click me" name="b1" onClick=tt();>
<br><br><br>
<select name="slt" size=5 multiple>
<option value=10>奔驰
<option value=21 >宝马
<option value=3>三菱
<option value=4>法拉利
<option value=5>宾利
<option value=6>丰田
<option value=7>别克
</select>
</form>
<hr>
<script language="JavaScript">
function tt(){
var op=document.ff.slt.options;
for(var i=0 ; i<7; i++){ //改用常量
document.write("###"+"<br>"); //字符串"###"
}
} </script>
<hr>
<p>驿外断桥边,寂寞开无主
</body> </html>
var op=document.ff.slt.options;
for(var i=0 ; i<7; i++){ //改用常量
document.write("###"+"<br>"); //字符串"###"
}
现在变成常量了
就是纯粹的打印7遍 和options没有一点关系
和什么值都没有关系