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

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

在新窗口中使用 javascript 更改 innerHTML

在新窗口中使用 javascript 更改 innerHTML

Smart貓小萌 2023-03-10 15:09:30
我正在嘗試使用下面的代碼通過 javascript 在另一個窗口中編輯列表項的 innerHTML。function searchNameButton() //this function is called when the user clicks the search by name button.{    name = document.getElementById("nmSearch").value;    window = window.open("search.html"); //opens a new window, can be accessed through this window variable    matchIndexes = [];    for (i = 0; i < pokemon.length; i++) //do the actual search here    {        if (pokemon[i][1].toLowerCase().includes(name.toLowerCase())) //converts to lowercase so that the search is case-insensitive        {             matchIndexes.push(i);        }    }    //now to populate the new page, similar to how it was done before    itemList = window.document.getElementsByClassName("pd");    console.log(matchIndexes);    for (i = 0; i < itemList.length; i++)    {        itemList[i].innerHTML = generateString(pokemon[matchIndexes[i]]);    }}但是,當新窗口打開時,沒有任何改變。我確定 matchIndexes 正在工作,我輸出了它的值,它在我的測試用例中找到了 3 個匹配項(應該如此),同樣,當我將它輸出到控制臺時,itemList 正確填充了 20 個項目。但是,更改任何這些項目的 innterHTML,即使作為測試在 for 循環(huán)之外進行,也無濟于事。我不確定我的錯誤到底是什么。generateString() 函數(shù),為了澄清,在其他地方工作正常,即使在最壞的情況下也不可能輸出空字符串。至少它會輸出一些我可以在檢查器中看到的字符。任何幫助,將不勝感激。
查看完整描述

1 回答

?
拉風的咖菲貓

TA貢獻1995條經(jīng)驗 獲得超2個贊

window.open()強調

請注意,遠程 URL 不會立即加載。返回時window.open(),窗口始終包含about:blank. 實際獲取 URL 會延遲并在當前腳本塊執(zhí)行完畢后開始。窗口創(chuàng)建和引用資源的加載是異步完成的。

由于Window似乎有一個onload處理程序,我會簡單地嘗試將窗口更改部分包裝到一個事件處理程序中:

function searchNameButton() //this function is called when the user clicks the search by name button.

{

    name = document.getElementById("nmSearch").value;

    wnd = window.open("search.html"); //opens a new window, can be accessed through this wnd variable

    matchIndexes = [];


    for (i = 0; i < pokemon.length; i++) //do the actual search here

    {

        if (pokemon[i][1].toLowerCase().includes(name.toLowerCase())) //converts to lowercase so that the search is case-insensitive

        { 

            matchIndexes.push(i);

        }

    }


    wnd.onload = function()

    {

        //now to populate the new page, similar to how it was done before

        itemList = wnd.document.getElementsByClassName("pd");

        console.log(matchIndexes);

        for (i = 0; i < itemList.length; i++)

        {

            itemList[i].innerHTML = generateString(pokemon[matchIndexes[i]]);

        }

    };

}

但是,我還沒有真正嘗試過這個,所以它可能會或可能不會工作。如果沒有,我會在處理程序的最開頭添加一些虛擬日志以查看它是否被調用,然后添加一行console.log(itemList);以查看getElementsByClassName()調用是否找到任何內容。


查看完整回答
反對 回復 2023-03-10
  • 1 回答
  • 0 關注
  • 118 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號