如何在不使用innerHTML的情况下在JavaScript中添加不间断的空格? [英] How do I add a non-breaking whitespace in JavaScript without using innerHTML?
本文介绍了如何在不使用innerHTML的情况下在JavaScript中添加不间断的空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在动态生成内容,在某些情况下,我需要将
设置为<span>
元素的唯一内容.
I'm generating content dynamically and in some instances, I need to set a
as the only content of a <span>
element.
但是,以下内容将
添加为文本,而不是添加空白:
However, the following adds
as text vs adding a empty space:
var foo = document.createElement("span")
foo = document.createTextNode(" ");
这很有意义,所以我想知道如何使用innerHTML
which makes sense, so I'm wondering, how would I add
correctly without (!) using innerHTML
感谢帮助!
推荐答案
You can use a unicode literal for a non breaking space:
var foo = document.createTextNode("\u00A0");
这篇关于如何在不使用innerHTML的情况下在JavaScript中添加不间断的空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文