zoukankan      html  css  js  c++  java
  • js的引入方式

    1)直接在HTML页面中使用<script></script>标签引入,可以写在head中,也可以写在body末尾,可以实现

    <code class="language-html"><!doctype html>  
    <html lang="en">   
    <head>    
        <meta charset="UTF-8">    
         
        <meta name="Generator" content="EditPlus®">    
        <meta name="Author" content="">    
        <meta name="Keywords" content="">    
        <meta name="Description" content="">    
        <title>js引入方式实验</title>   
    </head>   
    <body>                   
    <button id="button">实验</button>          
    <script>
        button.onclick = function () {
        alert("引入方法一:直接嵌入script标签");
        }
    </script>
         
    </body>  
    </html></code>  

    2).在html中使用script的src属性引入外部js文件,可实现。

    新建一个index1.html和index1.js文件,代码如下:

    <!doctype html>
    <html lang="en">
     <head>
      <meta charset="UTF-8">
      <meta name="Generator" content="EditPlus®">
      <meta name="Author" content="">
      <meta name="Keywords" content="">
      <meta name="Description" content="">
      <title>js引入方式实验</title>
     </head>
     <body>
            js的引入方式实验
            <button id="button">实验</button>
            <script src="index1.js">   
            </script>
     </body>
    </html>

    3).事件定义,直接在组件后面的事件中写javascript:js代码,可实现

    <!doctype html>
    <html lang="en">
     <head>
      <meta charset="UTF-8">
      <meta name="Generator" content="EditPlus®">
      <meta name="Author" content="">
      <meta name="Keywords" content="">
      <meta name="Description" content="">
      <title>js引入方式实验</title>
     </head>
     <body>
            js的引入方式实验
            <button id="button" onclick="javascript:alert('js引入的方式3:事件定义')">实验</button>
     </body>
    </html>

    4).在一个js文件中调用另外一个js 文件,可实现。但不能直接在第一个js文件中写<script src=xx.js></script>,这样写会导致引入js文件失败

    HTML文件中引入index1-1.js

    <!doctype html>
    <html lang="en">
     <head>
      <meta charset="UTF-8">
      <meta name="Generator" content="EditPlus®">
      <meta name="Author" content="">
      <meta name="Keywords" content="">
      <meta name="Description" content="">
      <title>js引入方式实验</title>
     </head>
     <body>
            js的引入方式实验
            <button id="button">实验</button>
            <script src="index1-1.js">
            </script>
     </body>
    </html>
  • 相关阅读:
    Android笔记之开机自启
    Android笔记之广播
    Hive笔记之collect_list/collect_set(列转行)
    Hive笔记之数据库操作
    hive笔记之row_number、rank、dense_rank
    Linux Shell管道调用用户定义函数(使shell支持map函数式特性)
    Linux shell爬虫实现树洞网鼓励师(自动回复Robot)
    分享一些免费的接码平台(国外号码)
    爬虫技能之内容提取:如何从有不可见元素混淆的页面中抽取数据
    ctf writeup之程序员密码
  • 原文地址:https://www.cnblogs.com/journey-mk5/p/9707449.html
Copyright © 2011-2022 走看看