Home  >  Article  >  Web Front-end  >  What are the three ways to use js? What's the difference

What are the three ways to use js? What's the difference

云罗郡主
云罗郡主forward
2018-10-22 16:42:392489browse

What this article brings to you is what are the three ways to use js? What is the difference? It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Inline js: js is not written separately

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>js使用方式1:行内js</title>
</head>
<body>
<input type="button" value="点击有惊喜" onclick="javascript:alert(&#39;哈哈哈哈&#39;)">
<!--onclick:点击触发一个事件,alert:弹出一个对话框-->
</body>
</html>

2. Internal js: the program in script can be used on the entire page

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>js使用方式2:内部js</title>
    <script type="text/javascript">
        //声明一个函数(整个文档都可以使用)
        function surprise() {
            alert(&#39;恭喜你中了一百万&#39;)/*弹出框*/
        }
    </script>
</head>
<body>
<input type="button" value="点击有惊喜" onclick="surprise()"><!--调用函数-->
<input type="button" value="点击" onclick="surprise()">
</body>
</html>

3. External js: Many html pages can call the set js page

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>js使用方式3:外部js</title>
    <!--很多html页面都可以调用js4.js页面-->
    <script src="../../js/js4.js" type="text/javascript" charset="utf-8">
    </script>
</head>
<body>
<input type="button" value="点击" onclick="test()">
</body>
</html>

What are the three ways to use js? All the differences are introduced. If you want to know more about JavaScript video tutorial, please pay attention to the PHP Chinese website.


The above is the detailed content of What are the three ways to use js? What's the difference. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete