如何在JavaScript字符串中表示不间断的空格? [英] How is a non-breaking space represented in a JavaScript string?

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

问题描述

这显然不起作用:

X = $td.text();
if (X == ' ') {
X = '';
}

有什么关于不间断的空间或JavaScript没有的&符号'喜欢?

Is there something about a non-breaking space or the ampersand that JavaScript doesn't like?

推荐答案

& nbsp; 是一个HTML实体。在执行 .text()时,所有HTML实体都会被解码为其字符值。

  is a HTML entity. When doing .text(), all HTML entities are decoded to their character values.

而不是使用实体,使用实际原始字符进行比较:

Instead of comparing using the entity, compare using the actual raw character:

var x = td.text();
if (x == '\xa0') { // Non-breakable space is char 0xa0 (160 dec)
  x = '';
}

或者您也可以在其Javascript中手动创建字符代码中的字符转义表格:

Or you can also create the character from the character code manually it in its Javascript escaped form:

var x = td.text();
if (x == String.fromCharCode(160)) { // Non-breakable space is char 160
  x = '';
}

有关 String.fromCharCode 可在此处获取:


fromCharCode - MDC Doc Center

有关字符代码的更多信息这里有不同的字符集:

More information about character codes for different charsets are available here:


Windows-1252 Charset

UTF-8 Charset

这篇关于如何在JavaScript字符串中表示不间断的空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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