[英]Unable to call a function from external js file
I have a button which calls the function showDiv()
when clicked. 我有一个按钮,单击后会调用函数
showDiv()
。
The function is declared in an external js file named script.js 该函数在名为script.js的外部js文件中声明
The function showDiv()
is not called when I use external file for the script. 当我使用外部文件作为脚本时,未调用函数
showDiv()
。 But by adding the script in the same html file I was successful . 但是通过将脚本添加到相同的html文件中,我成功了。
I am a beginner in javascript ,so wondering whats the reason for this. 我是javascript的初学者,所以想知道这是什么原因。
functions cant be added in external js files?What am i missing here? 无法在外部js文件中添加函数?我在这里缺少什么?
Here is my html code: 这是我的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>
Here is my script.js file: 这是我的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 "}];
}])
first make sure that your js file is accessible, so if you look into network tab in developer's tool(f12) it has to show 200 response, 首先请确保您的js文件可访问,因此,如果您在开发人员工具(f12)中查看“网络”标签,它必须显示200条响应,
in console tab check if you also dont have any js erros, the potential message should clarify reason 在控制台选项卡中检查您是否也没有任何js错误,潜在的消息应说明原因
Then you can use alert or console.log() as a first line in function to make sure that function is actually called 然后,您可以将alert或console.log()用作函数的第一行,以确保实际上已调用该函数
As mentioned -Is the table closed? 如上所述-桌子是否关闭? Try giving the full path to script.js -- https://myserver.org/scripts/script.js
尝试提供script.js的完整路径-https: //myserver.org/scripts/script.js
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.