Blogger Information
Blog 40
fans 1
comment 0
visits 32444
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
js的三种引入方式——2019年1月16日
李明伟的博客
Original
826 people have browsed it

js的三种引入方式


实例

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>js脚本的引入方式</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" media="screen" href="main.css" />
    <script src="main.js"></script>
</head>
<body>
        <!-- 1.直接在元素的事件方法属性上写js代码 -->
        <h3 id="tips" onclick="console.log(this.id)">js很好玩</h3>
        <!-- this可以省略,默认只想当前元素 -->
        <form action="">
            <input type="text" name="site" value="php中文网">
            <button type="button" onclick="console.log(site.value)">显示类型</button>
            <!-- 不同标签之间可以用标签的name属性连接 -->
            <input type="text" name="username" placeholder="用户名">
            <button type="button" onclick="check(username)">验证</button>
        </form>
</body>
</html>
<!-- 2.将js脚本写script标签中,仅限当前页面使用 -->
<script>
    function check(username){
        if(username.value.length === 0){
        alert("用户名空空如也!");
    }else{
        alert("验证通过");
    }
    }
</script> 
<!-- 3.将js脚本接到外部的js文件中 -->
<!-- <script src="js.js"></script> -->

运行实例 »

外联时的外联文件的代码:

function check(username){
if(username.value.length === 0){
alert("用户名空空如也!");
}else{
alert("验证通过");
}
}

点击 "运行实例" 按钮查看在线实例

  1. 直接在元素的事件方法属性上写js代码

  2. 将js脚本写script标签中,仅限当前页面使用

  3. 将js脚本接到外部的js文件中


Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!