How to preserve whitespace in dynamically added javascript DOM element without using CSS?

前端 未结 4 1582
傲寒
傲寒 2020-12-20 18:06

When adding in text with small whitespace appended to it for alignment purposes the whitespace is trimmed off (the whitespace is added in c# so by the time it gets to front

4条回答
  •  被撕碎了的回忆
    2020-12-20 18:22

    White space is collapsed in HTML. It's not a JS issue, the same would happen if you typed that manually in the HTML document. You need to replace the spaces with  

    zlp.innerHTML = "hello                hello".replace( / /g, " " );
    

提交回复
热议问题