字符串比较返回false. jQuery Mobile的奇怪JavaScript行为 [英] String comparison returns false. Strange javascript behaviour with jQuery mobile

查看:107
本文介绍了字符串比较返回false. jQuery Mobile的奇怪JavaScript行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了更好地学习它,我开始使用jQuery mobile创建一个待办事项列表.我正在听来自菜单按钮的事件,如下所示:

I started to create a todo list with jQuery mobile in order to learn it better. I'm listening to the events coming from the buttons of a menu like this:

        me.selectedTarget = me.menuNode.find(".ui-btn").first();

        me.menuNode.on("click", ".ui-btn", function (e) {
            var target =  $(e.currentTarget)
                ,targetText = target.text();
            console.debug("Click on'", targetText,"'" );
            if(target === me.selectedTarget) return;

            if(targetText == "View To Do "){
                core.pub("view:todo");
            } else if(targetText == "View Done ") {
                core.pub("view:done");
            } else {
                me.selectedTarget.click();
            }
            me.selectedTarget = target;
        });

由于某种原因,变量targetText在末尾有一个额外的"空间.我有3个按钮,但第三个按钮有点特殊-无论如何都不相关-.

The variable targetText has one extra " " space at the end for some reason. I have 3 buttons, but the third one is a bit special - not relevant anyway -.

我的问题是,我总是遇到最后一个else子句. targetText永远不会等于查看待办事项"或查看完成".

My problem is that I'm always getting on the last else clause. targetText never get's to be equal to "View To Do " or "View Done ".

为什么不显示"View Done" =="View Done"? 〜X(

Why isn't "View Done " == "View Done "? ~X(

推荐答案

由于字符串不同,比较失败.所以问题确实是为什么不一样?显然,其中一个字符串中的空格不是标准的空格字符(Unicode有多个空格),或者其中一个字符串中有一个不可见的字符(Unicode也有空格).

The comparison is failing because the strings are not the same. So the question really is why are they not the same? Apparently the space in one of the strings is not the standard space character (Unicode has more than one space), or one of the strings has an invisible character in it (Unicode has those, too).

因此,要找出正在发生的事情,我可以这样做:

So to find out what's going on, I'd do this:

var index;
for (index = 0; index < targetText.length; ++index) {
    console.log("char " + index + ": " + targetText.charCodeAt(index));
}

...以及与代码中的字符串文字相同的内容(不重新键入它,而是将其移到本地,然后输出该内容;因为如果重新键入,则可能会键入普通空格).比较字符代码的顺序,您会发现差异.

...and the same with your string literal in your code (not retyping it, but moving it into a local and then outputting that; since if you retype it, you'll presumably type the normal space). Compare the sequences of character codes and you'll find the discrepancy.

这篇关于字符串比较返回false. jQuery Mobile的奇怪JavaScript行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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