javascript
  • html
  • 2016-12-15 55 views 0 likes 
    0

    我有一個我從未遇到過的問題。我想簡單地在另一箇中添加一個div。我首先使用開發者控制檯進行測試。 我得到我想包含另一個div的元素,並在控制檯中正確打印元素。 後來我做到以下幾點:innerHtml無法在另一個內部打印我的div

    document.getElementById("ctiGlobalContainer").innerHtml += '<div id="ctiContentPanel" state="displayed" style="z-index: 10000;right: 0;top: 50px;height: 100%;width: 200px;position: fixed;margin: 0;"></div>' 
    

    我得到以下結果:

    "undefined<div id="ctiContentPanel" state="displayed" style="z-index: 10000;right: 0;top: 50px;height: 100%;width: 200px;position: fixed;margin: 0;"></div>" 
    

    爲什麼不確定的表現?而div不包括在內...

    +0

    這不是一個用innerHTML在你的頁面中添加HTML的最佳方式,請使用jQuery或,參見'.appendChild()'函數:http://stackoverflow.com/questions/14004117/create-div-and-append-動態分區 –

    回答

    2

    該物業被稱爲innerHTML

    innerHtml從未定義開始。

    +=將其轉換爲字符串,然後附加到它。

    當您將undefined轉換爲字符串時,您會得到"undefined"

    +0

    .....大寫字母.....謝謝 –

    0

    你在那裏有錯字。請代替innerHtml使用innerHTMLJavascript不承認innerHtml作爲其屬性,這就是爲什麼在那裏的代碼顯示undefined

    相關問題