当按钮被点击时如何运行JS代码



是否可以修改下面的代码,以便当您单击带有ID #slide-1-next的按钮时,javascript代码运行?理想情况下,代码在单击按钮之前不会运行。我不是超级精通JS,但我想也许最后一行代码与事件监听器可以修改来实现这一点?

let text = 'What follows is Lorem ipsum translated to English: But I must explain to you how all this mistaken idea of reprobating pleasure and extolling pain arose.';
function type() {
let textChar = text.charAt(textLength++);
let paragraph = document.getElementById("typed");
let charElement = document.createTextNode(textChar);
paragraph.appendChild(charElement);
if(textLength < text.length+1) {
setTimeout('type()', 50);
} else {
text = '';
}
}

document.addEventListener("DOMContentLoaded", function() {
type();
});

总结上面的注释,在代码块末尾添加以下行将在单击ID为slide-1-next的按钮时运行type函数:

document.getElementById("slide-1-next").onclick = type;

相关内容

  • 没有找到相关文章

最新更新