javascript - jquery html中缺少空格的字符索引

标签 javascript jquery

我正在尝试从元素的 html 中查找字符串中第一个空格的索引。在我简单地设置一个 test 变量并检查 indexOf(' ') 的测试用例中,它出现了。

例如,以下结果导致 test 的长度为 11 个字符,第一个空格位于第 5 个位置,这是应该的。

var test = 'hello there';
var idxTest1 = test.indexOf(' ');

但是,如果我将测试变量设置为来自 jQuery 元素 .html().text() 值,我将得到一个 - 1 用于空间的索引,即使 test 仍然看起来等于 'hello there',长度为 11 个字符。

这是怎么回事? http://jsfiddle.net/jhuTB/

最佳答案

注意, “不间断空格”不是空格键的典型空格。当观看 ascii 字符时,空格键的空间是 ASCII 32 (0x20),而 nbsp 是 ASCII 160 (0xA0),因此不等式。

关于javascript - jquery html中缺少空格的字符索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20992370/

相关文章:

javascript - Ember.js:如何在新项目插入 ArrayController 后应用 jquery 插件

javascript - 如何从通过 https 加载的网页与桌面应用程序通信

javascript - Angular js :ng-model is over writing the items in the ng-repeat

javascript - JQuery - 将变量值传递给类的索引值

jquery - 需要优化 jQuery 中隐藏和显示 div 的代码

javascript - 为 document.getElementById 和 document.createElement 使用变量

javascript - jQuery 解析 xml 并保持顺序

javascript - 如何在文件中写入文本/模板并将其包含到项目中

javascript - 单击时将文本元素转换为输入字段类型的文本,单击后又变回文本

jquery - 使用 jQuery 获取选择列表中第一个选项的值