第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

使用 JavaScript 函數(shù)將數(shù)據(jù)添加到來(lái)自 HTML 文檔的字符串

使用 JavaScript 函數(shù)將數(shù)據(jù)添加到來(lái)自 HTML 文檔的字符串

慕萊塢森 2023-12-04 16:37:12
我編寫了一個(gè)名為 TypeWriter2 的對(duì)象,然后我想為其添加一個(gè)名為 type2() 的函數(shù)。然后,我使用名為 init2() 的函數(shù)調(diào)用 TypeWriter2 對(duì)象,該函數(shù)從 html 文檔中查詢一些數(shù)據(jù)并將其傳遞給 TypeWriter2 對(duì)象。init2()從html文檔中查詢的數(shù)據(jù)是:txtElement2 = 一個(gè) div 元素,type2() 函數(shù)將使用它來(lái)顯示一些數(shù)據(jù)。words2 = txtElement2 中要顯示的單詞,即“Hello,there...Yoo”wait2 = 稍后將傳遞給 setTimeout() 的 int。type2() 函數(shù)的意思是,每當(dāng)“txt2”以 3 個(gè)連續(xù)點(diǎn)結(jié)尾時(shí),將“iiiiii”添加到“txt2”(開(kāi)頭為空字符串)。問(wèn)題在于,將“iiiiii”添加到“txt2”和“setTimeout(() => this.type2(), this.wait2);”之后 再次調(diào)用,“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 回答

?
翻翻過(guò)去那場(chǎng)雪

TA貢獻(xiàn)2065條經(jīng)驗(yàn) 獲得超14個(gè)贊

我無(wú)法使用發(fā)布的代碼重現(xiàn)該錯(cuò)誤,但很可能您可以通過(guò)將else語(yǔ)句更改為else if這樣來(lái)解決問(wèn)題:一旦“hello-txt”中的所有文本都停止調(diào)用 type2 方法“ 屬性已添加到txtElement2.innerHTML

嘗試重現(xiàn)案例: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);


}


查看完整回答
反對(duì) 回復(fù) 2023-12-04
  • 1 回答
  • 0 關(guān)注
  • 163 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)