[英]Javascript function not defined if from a separate file?
Here is my code: I am calling the function run() on the click of a button.这是我的代码:我通过单击按钮调用 function run()。 run() will call the function main() from the source code of bundle.js.
run() 将从 bundle.js 的源代码调用 function main()。 The console logs that the function main() is not defined.
控制台记录未定义 function main()。 I even tried running it in the console itself with no success.
我什至尝试在控制台本身中运行它,但没有成功。
<script type="text/javascript" src="bundle.js"></script>
<script type="text/javascript">
function run() {
main(document.getElementById('playlistUrl').value)
}
</script>
<input id="playlistUrl" placeholder="Enter a link...">
<button onclick="run()" id="go">Go!</button>
the function run() is defined, but not any funtion from bundle.js定义了 function run(),但没有来自 bundle.js 的任何功能
This is working fine,.这工作正常,。 check your file location or fails in bundle.js file
检查您的文件位置或在 bundle.js 文件中失败
//bundle.js file function main(value){ console.log(value); }
//main.js file function run() { main(document.getElementById('playlistUrl').value) }
<input id="playlistUrl" placeholder="Enter a link..."> <button onclick="run()" id="go">Go.</button> <script type="text/javascript" src="bundle.js"></script>
Code for index.html: index.html 的代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script type="text/javascript" src="bundle.js"></script>
</head>
<body>
<script type="text/javascript">
function run() {
main(document.getElementById('playlistUrl').value)
}
</script>
<input id="playlistUrl" placeholder="Enter a link...">
<button onclick="run()" id="go">Go!</button>
</body>
</html>
Code for bundle.js: bundle.js 的代码:
function main(){
document.write("Main ran")
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.