如何在不使用innerHTML的情况下在JavaScript中添加不间断的空格? [英] How do I add a non-breaking whitespace in JavaScript without using innerHTML?

查看:129
本文介绍了如何在不使用innerHTML的情况下在JavaScript中添加不间断的空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在动态生成内容,在某些情况下,我需要将&nbsp;设置为<span>元素的唯一内容.

I'm generating content dynamically and in some instances, I need to set a &nbsp; as the only content of a <span> element.

但是,以下内容将&nbsp;添加为文本,而不是添加空白:

However, the following adds &nbsp; as text vs adding a empty space:

var foo = document.createElement("span")
foo = document.createTextNode("&nbsp;");

这很有意义,所以我想知道如何使用innerHTML

which makes sense, so I'm wondering, how would I add &nbsp; correctly without (!) using innerHTML

感谢帮助!

推荐答案

您可以将unicode文字用于

You can use a unicode literal for a non breaking space:

var foo = document.createTextNode("\u00A0");

这篇关于如何在不使用innerHTML的情况下在JavaScript中添加不间断的空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆