admin管理员组文章数量:1134247
This apparently is not working:
X = $td.text();
if (X == ' ') {
X = '';
}
Is there something about a non-breaking space or the ampersand that JavaScript doesn't like?
This apparently is not working:
X = $td.text();
if (X == ' ') {
X = '';
}
Is there something about a non-breaking space or the ampersand that JavaScript doesn't like?
Share Improve this question edited Mar 24, 2023 at 11:06 Philipp Kyeck 18.8k19 gold badges88 silver badges125 bronze badges asked Mar 8, 2011 at 20:32 Phillip SennPhillip Senn 47.6k91 gold badges260 silver badges378 bronze badges 5 |4 Answers
Reset to default 405
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 = '';
}
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 = '';
}
More information about String.fromCharCode
is available here:
fromCharCode - MDC Doc Center
More information about character codes for different charsets are available here:
Windows-1252 Charset
UTF-8 Charset
Remember that .text()
strips out markup, thus I don't believe you're going to find
in a non-markup result.
Made in to an answer....
var p = $('<p>').html(' ');
if (p.text() == String.fromCharCode(160) && p.text() == '\xA0')
alert('Character 160');
Shows an alert, as the ASCII equivalent of the markup is returned instead.
That entity is converted to the char it represents when the browser renders the page. JS (jQuery) reads the rendered page, thus it will not encounter such a text sequence. The only way it could encounter such a thing is if you're double encoding entities.
The jQuery docs for text()
says
Due to variations in the HTML parsers in different browsers, the text returned may vary in newlines and other white space.
I'd use $td.html()
instead.
本文标签: jqueryHow is a nonbreaking space represented in a JavaScript stringStack Overflow
版权声明:本文标题:jquery - How is a non-breaking space represented in a JavaScript string? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736776694a1952375.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
.text()
strips out markup, thus I don't believe you're going to find
in a non-markup result. – Brad Christie Commented Mar 8, 2011 at 20:34