亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 JavaScript 函數將數據添加到來自 HTML 文檔的字符串

使用 JavaScript 函數將數據添加到來自 HTML 文檔的字符串

慕萊塢森 2023-12-04 16:37:12
我編寫了一個名為 TypeWriter2 的對象,然后我想為其添加一個名為 type2() 的函數。然后,我使用名為 init2() 的函數調用 TypeWriter2 對象,該函數從 html 文檔中查詢一些數據并將其傳遞給 TypeWriter2 對象。init2()從html文檔中查詢的數據是:txtElement2 = 一個 div 元素,type2() 函數將使用它來顯示一些數據。words2 = txtElement2 中要顯示的單詞,即“Hello,there...Yoo”wait2 = 稍后將傳遞給 setTimeout() 的 int。type2() 函數的意思是,每當“txt2”以 3 個連續點結尾時,將“iiiiii”添加到“txt2”(開頭為空字符串)。問題在于,將“iiiiii”添加到“txt2”和“setTimeout(() => this.type2(), this.wait2);”之后 再次調用,“iiiiii”將從“txt2”中刪除。document.addEventListener('DOMContentLoaded', init2);const TypeWriter2 = function (txtElement2, words2, wait2 = 3000) {    this.txtElement2 = txtElement2;    this.words2 = words2;    this.wait2 = parseInt(wait2, 10);    this.txt2 = '';    this.type2();}TypeWriter2.prototype.type2 = function () {    this.txt2 = this.words2.substring(0, this.txt2.length + 1)    if (this.txt2.substr(this.txt2.length - 3) === "...") {        this.txt2 = this.txt2 + "iiiii"        this.txtElement2.innerHTML = `<span class="intro-text">${this.txt2}</span>`;    } else {        this.txtElement2.innerHTML = `<span class="intro-text">${this.txt2}</span>`;   }    setTimeout(() => this.type2(), this.wait2);}function init2() {    const txtElement2 = document.querySelector('.intro-text');    const words2 = txtElement2.getAttribute('hello-txt');    const wait2 = txtElement2.getAttribute("data2-wait");    new TypeWriter2(txtElement2, words2, wait2);}提前致謝!
查看完整描述

1 回答

?
翻翻過去那場雪

TA貢獻2065條經驗 獲得超14個贊

我無法使用發布的代碼重現該錯誤,但很可能您可以通過將else語句更改為else if這樣來解決問題:一旦“hello-txt”中的所有文本都停止調用 type2 方法“ 屬性已添加到txtElement2.innerHTML

嘗試重現案例:https://jsbin.com/wovatit/1/edit? html,js,output

document.addEventListener('DOMContentLoaded', init2);


const TypeWriter2 = function (txtElement2, words2, wait2 = 3000) {

    this.txtElement2 = txtElement2;

    this.words2 = words2;

    this.wait2 = parseInt(wait2, 10);

    this.txt2 = '';

    this.type2();

}


TypeWriter2.prototype.type2 = function () {

  console.log('called');

    this.txt2 = this.words2.substring(0, this.txt2.length + 1)


    if (this.txt2.substr(this.txt2.length - 3) === "...") {

        this.txt2 = this.txt2 + "iiiii"

        this.txtElement2.innerHTML = `<span class="intro-text">${this.txt2}</span>`;

        console.log("finished")

    } else if(this.txt2.length <= this.words2.length){

        this.txtElement2.innerHTML = `<span class="intro-text">${this.txt2}</span>`;

        setTimeout(() => this.type2(), this.wait2);

    } else{

      console.log("finsished")

    }



}



function init2() {

    const txtElement2 = document.querySelector('.intro-text');

    const words2 = txtElement2.getAttribute('hello-txt');

    const wait2 = txtElement2.getAttribute("data2-wait");

    new TypeWriter2(txtElement2, words2, wait2);


}


查看完整回答
反對 回復 2023-12-04
  • 1 回答
  • 0 關注
  • 153 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號