在html页面上嵌入JavaScript代码的几种方式

来源:互联网 发布:小强软件测试 编辑:程序博客网 时间:2024/04/20 02:37

 代码:

1.内部写入JavaScript程序

  1.1

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>我的第一个JavaScript程序</title></head><body>  <a href="JavaScript:alert('Hello,JavaScript!');">点击我</a></body></html>

   1.2

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>我的第一个JavaScript程序</title><script type="text/javascript"> alert('Hello,JavaScript!');</script></head><body>  <a href="JavaScript:alert('Hello,JavaScript!');">点击我</a></body></html>

2.外部导入JavaScript程序

  2.1

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>我的第一个JavaScript程序</title><script src="outer.js" type="text/javascript"></script></head><body>  <a href="JavaScript:alert('Hello,JavaScript!');">点击我</a></body></html>

  2.1

 outer.js

 alert('Hello,JavaScript!');


2.2

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>我的第一个JavaScript程序</title><script src="outer.js" type="text/javascript"></script></head><body>  <a href="JavaScript:show();">点击我</a></body></html>

outer.js

// JavaScript Document alert('Hello,JavaScript!');  function show(){ alert('Hello,JavaScript!'); }

制作截图:


知识点总结:







0 0
原创粉丝点击