index.html:
<script type="module" src="./index.js"></script>
<body>
<button onclick="myFunc()">click</button>
</body>
<script type="module">
import Dog from './dog.js';
let dog = new Dog();
function myFunc(){
dog.doSomething();
}
</script>
脚本无法调用,因为我有 type="module"。如果我没有模块,我就无法访问 Dog 类。如何将 html 事件与模块连接起来?
最佳答案
使用 addEventListener
从 JavaScript 绑定(bind)事件处理程序.
document.querySelector("button").addEventListener("click", myFunct);
不要使用内部事件属性。
关于javascript - 如何在使用 <script type ="module"> 加载的 HTML 中调用函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46886930/