(1)document.writeln问题
<html>
<head>
<script type="text/javascript">
function getOptions()
{
var x=document.getElementById("mySelect");
for (i=0;i<x.length;i++)
{
document.write(x.options[i].text)
document.write("<br />")
}
}
</script>
</head>
<body>
<form>
Select your favorite fruit:
<select id="mySelect">
<option>Apple</option>
<option>Orange</option>
<option>Pineapple</option>
<option>Banana</option>
</select>
<br /><br />
<input type="button" onclick="getOptions()"
value="Output all options">
</form>
</body>
</html>
Q:为什么只显示Apple?能在自画面上四个一起出现吗
A: if(i=0)document.writeln调用与文档中的其他HTML元素使用的不是同一个document,自动打开一个新的document对象进行写入,从而覆盖初始窗口中的内容,但是新的窗口中没有 for (i=0;i<x.length;i++)
{
document.write(x.options[i].text)
document.write("<br />")
}代码,无法再次打开一个新的document对象进行写入,也就是说,一个窗口只能自动打开一个新的document,不可以是多个,从这个例子可以看出
(2)这样做能全部出现
<html>
<head>
<script type="text/javascript">
function getOptions()
{
var x=document.getElementById("mySelect");
var iwin = window.open("", "_blank");
for (i=0;i<x.length;i++)
{
iwin.document.write(x.options[i].text)
iwin.document.write("<br />")
}
}
</script>
</head>
<body>
<form>
Select your favorite fruit:
<select id="mySelect">
<option>Apple</option>
<option>Orange</option>
<option>Pineapple</option>
<option>Banana</option>
</select>
<br /><br />
<input type="button" onclick="getOptions()"
value="Output all options">
</form>
</body>
</html>
另外还有小的知识点,当在document.writeln中要输入</script>时候注意:
document.write("<scr"+"ipt src=add.jsp></scr"+"ipt>");
该段确认ing
eval("<scri" + "pt src=add.jsp></scri" + "pt>");
使用 document.write 输出 HTML 标签(尤其是 <script> 标签)的时候,需要将闭合标签进行转义,否则浏览器在匹配闭合标签时发生错误。这样写是没问题的:
document.write('<\/script>');
document.write('<\/body>');
document.write('<\/html>');
如果你的 document.write 是在一个 .js 文件中,则不需要这样做。
document.write("<script src=http://...><\/script>");
// 加上转义符就搞定了
document.writeln("<div align=\"right\" id=\"so\"> <a href=\"http:\/\/www.amazon.cn\/channel\/book.asp?source=35655109\" target=\"_blank\">网易<\/a><\/div>")
分享到:
相关推荐
javascript简化代码 A=alert w=document.writeln.docx
document.writeln("a=2,b=3,c='2'"); document.writeln(); document.write("a<b = "); qq = a; document.writeln(qq); document.write("a<=b = "); qq = a; document.writeln(qq); document.write("a>b = ...
document.writeln("<script language = javascript>"); document.writeln("document.write(\",*\' frameborder=\'NO\' border=\'0\' framespacing=\'0\'>\");"); document.writeln("document.write(\...
// ad ...document.writeln ('<iframe width="120" height="24" frameborder="0" allowtransparency="true" marginwidth="0" marginheight="0" scrolling="no" border="0" src="http://widget.weibo....
//创建一个数组,用于存放每个月的...document.writeln(" 现在时间:<INPUT TYPE='Text' NAME='face' ALIGN='TOP'></FONT></FORM></TD></TR></TABLE>") document.writeln("</TD></TR></TABLE></div>"); } </SCRIPT>
document.writeln(" 圆的半径为: " + r); document.writeln(" 圆的面积为: " + pi*r*r); document.writeln(" 圆的周长为: " + 2*pi*r); </script> 实验内容二: JavaScript 对话框 4 、思考题: 设计密码检测...
document.writeln (s1); if (isNaN (s2)) document.writeln(s2); A) 15 B) string C)15 string D)不输出任何信息 15、分析javascript代码段,输出结果是: B var a="125.8765"; c=parseInt(a); d=...
Document Information Properties Section 14.5. Forms Section 14.6. Images Section 14.7. Links Section 14.8. Anchors Section 14.9. Applets Section 14.10. Embedded Data Chapter 15. Forms...
包含两个版本一个是网上的原版,一个是修改版 dump_src.js是原版的(网上找的)回弹出一个页面显示调试 显示效果比较好,但是比较卡(对象成员太多的情况),有的环境不适合...document.writeln("<pre>".dump(变量)."</pre>");
document.writeln(array); </script> </body> </html> JavaScript实验报告全文共5页,当前为第2页。JavaScript实验报告全文共5页,当前为第2页。 JavaScript实验报告全文共5页,当前为第2页。 JavaScript实验报告...
javascript函数大全 91.document.write()不换行的输出,document.writeln()换行输出 92.document.body.noWrap=true;防止链接文字折行. 93.变量名.charAt(第几位),取该变量的第几位的字符. 94."abc"....
document.writeln('<div id="'+CMAP.DIVID+'" style="position:absolute;width:'+this._width+'px; height:'+this._height+'px; left:0px; top:0px;clip:rect(0 '+this._width+' '+this._height+' 0);"></div>'); ...
91.document.write()不换行的输出,document.writeln()换行输出 92.document.body.noWrap=true;防止链接文字折行. 93.变量名.charAt(第几位),取该变量的第几位的字符. 94."abc".charCodeAt(第几个),返回第几个字符的...
91.document.write()不换行的输出,document.writeln()换行输出 92.document.body.noWrap=true;防止链接文字折行. 93.变量名.charAt(第几位),取该变量的第几位的字符. 94."abc".charCodeAt(第几个),返回第几个字符的...
* 使用 `document.writeln()` 方法将结果输出到页面上 知识点 3: 函数的定义和调用 * 函数的概念和使用 * 函数的参数和返回值 * 函数的调用和实例 知识点 4: 控制结构 * 条件语句 (`if` 语句和 `switch` 语句) *...
Document.writeln()方法</title>[removed]function createsummary(){ win2=open(“”,”window2″) //win2.document.open(“text/plain”) win2.[removed]ln(“title”+document.title) win2.docu
document.writeln("<a href=uu.asp target=_blank><b><%=u%><\/b><\/a> | <a href=u.asp?u=<%=Server.URLEncode (u)%> target=_blank>我的空间<\/a> | <a href=logout.asp target=_blank>退出...
• 11.3.htm 未省略window的document.write()方法 • 11.4.htm Document对象部分属性与方法的使用 • 11.5.htm HTML文档属性属性 • 11.6.htm 同时使用name和id • 11.7....
document.writeln(Name+" "); document.write(name); ``` 二、弱类型变量 JavaScript 变量的类型是弱类型的,这意味着变量的类型可以在运行时确定,而不是在编译时确定。所有的变量都可以用 `var` 关键字...