打字机 JS - .typeString 无法正常工作
Typewriter JS - .typeString not working properly
我使用了一个叫做 Typewriter JS 的小框架。我的猜测是事件监听器无法访问打字机变量,但我不知道为什么以及如何修复它。
var app = document.getElementById('app');
var typewriter = new Typewriter(app, {});
document.addEventListener("keyup", function(event) {
if (event.key=="Enter") {
typewriter.typeString('this does not work');
console.log("but this works");
}
});
typewriter.start();
typewriter.typeString('this works');
<script src="https://unpkg.com/typewriter-effect@latest/dist/core.js"></script>
<div id="app"></div>
每次都需要停止和启动它:
var app = document.getElementById('app');
var typewriter = new Typewriter(app, {});
document.addEventListener("keyup", function(event) {
if (event.key == "Enter") {
typewriter.stop();
typewriter.typeString('this does not work');
typewriter.start();
console.log("but this works");
}
});
typewriter.start();
typewriter.typeString('this works');
<script src="https://unpkg.com/typewriter-effect@latest/dist/core.js"></script>
<div id="app"></div>
我使用了一个叫做 Typewriter JS 的小框架。我的猜测是事件监听器无法访问打字机变量,但我不知道为什么以及如何修复它。
var app = document.getElementById('app');
var typewriter = new Typewriter(app, {});
document.addEventListener("keyup", function(event) {
if (event.key=="Enter") {
typewriter.typeString('this does not work');
console.log("but this works");
}
});
typewriter.start();
typewriter.typeString('this works');
<script src="https://unpkg.com/typewriter-effect@latest/dist/core.js"></script>
<div id="app"></div>
每次都需要停止和启动它:
var app = document.getElementById('app');
var typewriter = new Typewriter(app, {});
document.addEventListener("keyup", function(event) {
if (event.key == "Enter") {
typewriter.stop();
typewriter.typeString('this does not work');
typewriter.start();
console.log("but this works");
}
});
typewriter.start();
typewriter.typeString('this works');
<script src="https://unpkg.com/typewriter-effect@latest/dist/core.js"></script>
<div id="app"></div>