是否可以修改以下代碼,以便當您單擊 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();
});
uj5u.com熱心網友回復:
總結上面的評論,在代碼塊末尾添加以下行將在單擊type帶有 ID 的按鈕時運行該函式slide-1-next:
document.getElementById("slide-1-next").onclick = type;
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/338723.html
標籤:javascript
