我有一个带有脚本标记的HTML文件。为了重用其他页面通用的函数,我创建了一个包含一些函数的外部JS文件。现在,如果我链接头部的外部JS文件,并尝试使用onclick="functionname()“访问函数作为HTML标签的属性,它就可以工作了。但是我想在script标记内调用外部JS文件中的函数(我需要将参数传递给函数,并在script标记内提取这些值),但我不能这样做。是否可以在script标记内调用外部文件中的函数?若有,如何处理?
发布于 2021-07-14 16:05:02
script tagonClick="functionName()" in your html添加html文件
确保您使用的函数必须存在于index.js文件中
检查下面的代码
<body>
<script type="text/javascript" src="index.js"></script>
<script type="text/javascript">
function call() {
showAlert()
}
</script>
<button onclick="call()">Call External Function</button>
</body>首先加载外部js文件,如下所示
<script type="text/javascript" src="index.js"></script>添加新的脚本标记并添加另一个函数来调用外部函数
<script type="text/javascript">
function callExternalFun() {
showAlert()
}
</script>然后在你的超文本标记语言中添加callExternalFun()
<button onclick="callExternalFun()">Call External Function</button>包含showAlert函数的外部JS代码index.js
function showAlert() {
alert("External JS Function")
}https://stackoverflow.com/questions/68372234
复制相似问题