zoukankan      html  css  js  c++  java
  • 如何引入(调用)一个 js文件

    我们旺旺需要调用别的 js文件。怎么处理?

    看随机抽取这个例子。在一个页面中如下:

    <html>
    <head>
    <title>random number</title>
    <script type="text/javascript">
        //随机抽取人名    </script>
    </head>
    
    <body>
        <form>
            <input type="button" style='font-size:40px' value="Start" onclick="start()">
            <input type="button" style='font-size:40px' value="Stop" onclick="stop();">
        </form>
            <br>&nbsp;&nbsp;&nbsp;&nbsp;
            <font color="blue" style='font-size:150px' id="num"></font>
        <br>
    </body>
    </html>

    我们可以把 js 放在另外一个文件里,比如当前文件夹的 a.js 中。

    这样 html 页面如下:

    <html>
    <head>
    <title>random number</title>
    <script type="text/javascript" src="a.js">
    </script>
    </head>
    
    <body>
        <form>
            <input type="button" style='font-size:40px' value="Start" onclick="start()">
            <input type="button" style='font-size:40px' value="Stop" onclick="stop();">
        </form>
            <br>&nbsp;&nbsp;&nbsp;&nbsp;
            <font color="blue" style='font-size:150px' id="num"></font>
        <br>
    </body>
    </html>

    a.js

        var errorString = "Please input a positive integer.";
        var arr = ["A", "B", "C", "D"];
        
        function count() {
            max = arr.length; //max, 全局变量
            document.getElementById("num").innerHTML = arr[parseInt(max * Math.random())];
        }
    
        function start()
        {
            timeId = setInterval("count();", 100);
        }
        function stop() {
            clearInterval(timeId);
        }

    这样就行了。

    当然,也可以把 a.js放在web上,然后引用成下面这样。

    <html>
    <head>
    <title>random number</title>
    <script type="text/javascript" src="http://localhost:8080/test/js/random1.js"></script>
    </head>
    
    <body>
        <form>
            <input type="button" style='font-size:40px' value="Start" onclick="start()">
            <input type="button" style='font-size:40px' value="Stop" onclick="stop();">
        </form>
            <br>&nbsp;&nbsp;&nbsp;&nbsp;
            <font color="blue" style='font-size:150px' id="num"></font>
        <br>
    </body>
    </html>

    Ok

  • 相关阅读:
    react的50个面试题
    什么是宏队列跟微队列
    宏队列与微队列
    数组都有哪些方法
    vuex 跟 vue属性
    高阶组件
    如何创建视图簇(View cluster)-SE54/SM34
    ◆◆0如何从维护视图(Maintenace view)中取数据-[VIEW_GET_DATA]
    ◆◆0如何在SM30维护表时自动写入表字段的默认值-事件(EVENT)
    ◆◆0SAP Query 操作教程
  • 原文地址:https://www.cnblogs.com/backpacker/p/2645023.html
Copyright © 2011-2022 走看看