Advertisement

JavaScript系列笔记——javascript之helloworld

阅读量:

一、在哪里写Javascript代码
1、在内部文件中写
在html文档中标签中添加

复制代码
    <!DOCTYPE html>
    <html>
    <head>
    <title>Document</title>
    **<script>
        alert('hello world!');
    </script>**
    </head>
    <body>
    </body>
    </html>
    
    
      
      
      
      
      
      
      
      
      
      
      
    
    AI写代码

alter警告框
alter(“需要显示的内容”);

在这里插入图片描述

我们可以注意到在网页上出现了一个显示"hello world!"文字的提示框。
我们选择在外部文件中进行编写。
编写一个扩展名为.js的JavaScript脚本文件。
alert("hello world");

在这里插入图片描述

我们已经在js文件中完成了相关代码模块, 但目前我们仍需思考如何将它们应用到网页上.
为此, 我们需要在html文档中导入外部的js文件.

复制代码
    <!DOCTYPE html>
    <html>
    <head>
    <title>Document</title>
    <script src="js测试.js" type="text/javascript"></script>
    </head>
    <body>
    </body>
    </html>
    
    
      
      
      
      
      
      
      
      
      
    
    AI写代码

同样我们可以得到

在这里插入图片描述

在HTML文档中, 我们会创建一个按钮, 当用户点击该按钮时, 网页就会显示带有'hello world'信息的提示框。

复制代码
    <!DOCTYPE html>
    <html>
    <head>
    <title>Document</title>
      
    </head>
    <body>
    <button onclick="alert('hello word!')">点我一下试试</button>
    <a href="javascript:alter('hello world!')">我是超链接</a>
    </body>
    </html>
    
    
      
      
      
      
      
      
      
      
      
      
      
    
    AI写代码
在这里插入图片描述

这时我们点击按钮和超链接都会出现上面同样的警告框

在实际操作过程中总结发现,在这种情况下我们强烈建议采用第二种外部引入的方式。因为这种方案不仅实现了结构与行为的分离,并且同样能够将相同的JS代码应用于多个网页以提高性能。

全部评论 (0)

还没有任何评论哟~