Yus*_*nif 2 javascript import function
import testBtn from '/functions.js'
window.onload = function() {
document.getElementById('questions1').style.display = 'block'
document.getElementById('questions2').style.display = 'none'
document.getElementById('questions3').style.display = 'none'
document.getElementById('questions4').style.display = 'none'
document.getElementById('questions5').style.display = 'none'
}
function nextQuestion() {
testBtn("hello")
}
Run Code Online (Sandbox Code Playgroud)
当我尝试导入或将脚本类型更改为type="module"函数时,所有函数都不起作用,当所有函数都没有导入或脚本类型更改时,我收到以下错误 -Uncaught ReferenceError: nextQuestion is not defined at HTMLButtonElement.onclick
ECMAScript 模块不使用全局作用域,因此onclick="nextQuestion"HTML 元素上的属性将无法找到nextQuestion,因为它不在全局作用域中,而是在模块作用域中(与 一起使用时type="module")。要解决此问题,请将nextQuestion函数放入全局范围:
window.nextQuestion = function() {
testBtn("hello")
}
Run Code Online (Sandbox Code Playgroud)