我有我的网站,其中的脚本可以在 span
中的 h2
之后输入一系列内容。
首先,我必须将脚本包含在 html 文件中,整个 js 脚本,这在 html 文件中也太长而且太无用。 所以我试图将它放在我的 html 文件之外,并将其包含在内,就像这样。
<script src="javascript/index.js"></script>
但是我的 span
没有任何反应
<h2>I am <span id="typing"></span></h2>
这是我的index.js
var typed = new Typed('#typing', {
strings: ['first thing', 'second thing'],
typeSpeed: 30,
loop: true
});
最佳答案
您需要等待 dom 加载。
document.addEventListener("DOMContentLoaded", function(event) {
var typed = new Typed('#typing', {
strings: ['first thing', 'second thing'],
typeSpeed: 30,
loop: true
});
});
关于javascript - 如何将打字脚本放在 html 文件之外?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58684469/