Setting innerHTML: Why won't it update the DOM?

前端 未结 5 952
伪装坚强ぢ
伪装坚强ぢ 2020-11-29 08:37

Wondering why I can\'t get document.getElementById(\"my_div\").innerHTML to update the DOM when I re-assign the variable. For example:

5条回答
  •  难免孤独
    2020-11-29 09:24

     var myDivValue = document.getElementById("my_div").innerHTML;
    

    stores the value of innerHTML, innerHTML contains a string value, not an object. So no reference to the elem is possible. You must to store directly the object to modify its properties.

    var myDiVElem = document.getElementById("my_div");
    myDiVElem.innerHTML = 'Hello'; // this makes the change
    

提交回复
热议问题