簡體   English   中英

使用document.getElementById時如何刪除和替換div中的內容

[英]How to remove and replace content in a div when using document.getElementById

我正在使用此javascript將數組中的文本放入div

function showTopic(j)   
{   
        document.getElementById("topicBox").innerHTML += 

        '<h2>'+ arrSubTopics[j].subCategoryName   +'</h2>

         '+ arrSubTopics[j].subCategoryInformation +' ';          
 }

這只會添加到div ,但我希望它替換div當前存在的任何內容

建設性的批評,而不是請投反對票。

使用=代替+=

document.getElementById("topicBox").innerHTML = '<h2>' // the rest...

替換+==

document.getElementById("topicBox").innerHTML = ...

+=表現為字符串串聯,盡管您在這里不需要它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM