javascript

来源:互联网 发布:咕咕收音机 mac 编辑:程序博客网 时间:2024/06/07 23:44
1  <html> <body> <script type="text/javascript"> document.write("Hello World!"); </script> </body> </html> 结果 Hello World! 2 <html> <head> <script type="text/javascript"> function message() { alert("该提示框是通过  onload  事件调用的。 ") } </script> </head> <body onload="message()"> </body> </html> 结果     3 Javascript可以放在三个地方 <head>标签内: 当脚本被调用时,或者当事件被触发时,脚本就会被执行。当你把脚本放置到 head  部分后,就可以确保在需要使用脚本之前,它已经被载入了。 <body>标签内: 在页面载入时脚本就会被执行。当你把脚本放置于 body  部分后,它就会生成页面的内容。 外部:有时,你也许希望在若干个页面中运行 JavaScript ,同时不在每个页面中写相同的脚本。 为了达到这个目的,你可以将 JavaScript  写入一个外部文件之中。然后以  .js  为后缀保存这个文件。 <html> <head> </head> <body> <script src="/js/example_externaljs.js"> </script> <p> 实际的脚本位于名为 "xxx.js"  的外部脚本中。 </p> </body> </html> 输出 实际的脚本位于名为 "xxx.js"  的外部脚本中。 4 <script type="text/javascript"> document.write("<h1>This is a header</h1>"); document.write("<p>This is a paragraph</p>"); document.write("<p>This is another paragraph</p>"); </script> 输出 This is a header This is a paragraph This is another paragraph 5 <script type="text/javascript"> { document.write("<h1>This is a header</h1>"); document.write("<p>This is a paragraph</p>"); document.write("<p>This is another paragraph</p>"); } </script> 输出 This is a header This is a paragraph This is another paragraph