JavaScript程序设计基础教程实训指导-7-1 实训案例名称:运用document对象属性和方法.docx
实训案例名称:运用document对象属性和方法
1.任务介绍
用JavaScript编写一个动态生成文档示例。
2.任务目标
学会document对象属性和方法的运用。
3.实现思路
使用open()、close()、write()和writeln()方法动态创建在浏览器窗口中显示的文档。
4.实现代码
完整代码如脚本7-6所示。
脚本7-6.html
!DOCTYPEhtml
html
head
title动态生成文档示例/title
scripttype=text/javascript
functionopenNewWin(){
varnewWin=open(,myWindow,width=400,height=100,left=0,top=10,+
toolbar=no,menubar=no,+scrollbars=no,resizable=no,location=no,status=no);
newWin.document.open();
newWin.document.writeln(htmlheadtitle通知/title/head);
newWin.document.writeln(body);
newWin.document.writeln(h1通知/h1);
newWin.document.writeln(p请同学们注意,下周举行计算机考试!/p);
newWin.document.writeln(/body/html);
newWin.document.close();
newWin.focus();
}
/script
/head
body
pahref=javascript:openNewWin()通知/a/p
图7.5
图7.5动态生成文档示例结果
/html
5.运行结果
运行结果如图7.5所示。