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

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

如何在特定 div 中強(qiáng)制運(yùn)行 JS 函數(shù)

如何在特定 div 中強(qiáng)制運(yùn)行 JS 函數(shù)

慕哥6287543 2022-08-04 10:34:44
我正在嘗試用js構(gòu)建表,但我無(wú)法強(qiáng)制它在特定的div中構(gòu)建,它總是在整個(gè)部分之外創(chuàng)建我的表。有什么辦法可以強(qiáng)迫它嗎?網(wǎng)頁(yè):<section class="all-products" id="all-products">    <div class="container">        <div class="title-box">            <h2>Products</h2>        </div>        <div class="row" id=products-row">            <script>              LoadAllProducts();            </script>        </div>    </div></section>負(fù)載產(chǎn)品.js:function LoadAllProducts() {    var x = document.createElement("TABLE");    x.setAttribute("id", "table");    document.body.appendChild(x);    var y = document.createElement("TBODY");    y.setAttribute("id", "products");    document.getElementById("table").appendChild(y);}
查看完整描述

2 回答

?
蕭十郎

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

問(wèn)題是您正在調(diào)用 .appendChild()body


function LoadAllProducts() {

    var x = document.createElement("TABLE");

    x.setAttribute("id", "table");

    // Instead of document.body, use document.getElementById()

    document.getElementById("products-row").appendChild(x);


    var y = document.createElement("TBODY");

    y.setAttribute("id", "products");

    // ... like you were using here...

    document.getElementById("table").appendChild(y);

}


函數(shù)調(diào)用在HTML中的位置并不重要,都會(huì)引用當(dāng)前頁(yè)面的元素。document.bodybodydocument


查看完整回答
反對(duì) 回復(fù) 2022-08-04
?
守候你守候我

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

 document.body.appendChild(x);

顯然是 document.body。該程序正在完全按照您的指示執(zhí)行操作...將節(jié)附加到文檔的正文。

該程序不理解“含義”,例如“我把這個(gè)代碼放在這里,所以我期望代碼在這里輸出”。JavaScript DOM 不是這樣工作的。您必須指定輸出的去向。

如果你想讓它出現(xiàn)在產(chǎn)品行中,你必須告訴它去那里

document.getElementById("products-row").appendChild(x)

在這種情況下,我們通過(guò)id獲取元素“products-rows”,然后告訴代碼“在這里附加我的輸出x”。


查看完整回答
反對(duì) 回復(fù) 2022-08-04
  • 2 回答
  • 0 關(guān)注
  • 195 瀏覽
慕課專欄
更多

添加回答

舉報(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)