无法从外部js文件

时间:2017-02-28 22:18:13

标签: javascript html angularjs function

我有一个按钮,在点击时调用函数showDiv()

该函数在名为script.js

的外部js文件中声明

当我使用外部文件作为脚本时,不会调用函数showDiv()。 但是通过在同一个html文件中添加脚本,我获得了成功。

我是javascript的初学者,所以想知道这是什么原因。

函数不能添加到外部js文件中?我在这里缺少什么?

这是我的HTML代码:

<button type="button"  class="btn btn-primary btn-lg "  onclick="showDiv()" >view</button>

    <br/>
    <br/>
    <div id="container" ng-controller="ctrl" style="display:none;">
            <table ng-table="table condensed"  align="center">

                <thead>
                    <tr>
                        <th>RUNNO</th>
                        <th>CWAM</th>
                        <th>HWAM</th>
                        <th>CNAM</th>
                        <th>DPNAM</th>
                        <th>OCAM</th>

                    </tr>
                </thead>
                <tbody>
                    <tr ng-repeat="value in ourArray">
                        <td>{{value.RUNNO}}</td>
                        <td>{{value.CWAM}}</td>
                        <td>{{value.HWAM}}</td>
                        <td>{{value.CNAM}}</td>
                        <td>{{value.DPNAM}}</td>
                        <td>{{value.OCAM}}</td>

                    </tr>
                    </tbody>
    </div>




    <script type="text/javascript" src="script.js"></script>

这是我的script.js文件:

function showDiv() {
   document.getElementById('container').style.display = "block";
}

var app=angular.module("app",[])
app.controller('ctrl', ['$scope', function($scope){
    $scope.ourArray=[{"RUNNO":"1","CWAM":"7615","HWAM":"2848","CNAM":"67","DPNAM":"-99","OCAM":"78603"},
                     {"RUNNO":"2","CWAM":"10018","HWAM":"4150","CNAM":"94","DPNAM":"167.0 ","OCAM":"78608 "},
                     {"RUNNO":"3","CWAM":" 11087 ","HWAM":"4416 ","CNAM":"117","DPNAM":"61.6","OCAM":" 78612"},
                     {"RUNNO":"4","CWAM":"5895 ","HWAM":"1737","CNAM":"44","DPNAM":"-99","OCAM":"78556"},
                     {"RUNNO":"5","CWAM":"7787","HWAM":"2338","CNAM":"59","DPNAM":"129.8 ","OCAM":" 78556 "},
                     {"RUNNO":"6","CWAM":"9610","HWAM":"3519 ","CNAM":"83","DPNAM":" 53.4","OCAM":" 78560 "}];

}])

2 个答案:

答案 0 :(得分:0)

首先确保您的js文件可以访问,因此如果您查看开发人员工具(f12)中的网络标签,则必须显示200响应,

在控制台选项卡中检查您是否也没有任何js错误,潜在的消息应该澄清原因

然后你可以使用alert或console.log()作为函数的第一行,以确保实际调用该函数

答案 1 :(得分:0)

如上所述 - 桌子关闭了吗? 尝试提供script.js的完整路径 - https://myserver.org/scripts/script.js