等于(==)和等于(===)对我不起作用

Dan*_*lik 0 hash jquery equals-operator

我正在使用if条件在jquery和==不适合我也不===。:(这是我的代码:

var hash = window.location.hash;
var hash = hash.replace('#', ' ');
  alert(hash); /* Returns abc.*/
if( hash === 'abc'){
            alert(hash); /* not coming here.*/
        $('.tab').removeClass('is-active');
    }
Run Code Online (Sandbox Code Playgroud)

有什么帮助吗?提前致谢。

Ran*_*osh 5

window.location.hash将返回#abcabc。因此,替换以下代码::

var hash = window.location.hash;
Run Code Online (Sandbox Code Playgroud)

有了这个::

var hash = window.location.hash.split('#')[1];
Run Code Online (Sandbox Code Playgroud)

完整代码将是:

var hash = window.location.hash.split('#')[1];
if( hash === 'abc'){
    $('.tab').removeClass('is-active');
}
Run Code Online (Sandbox Code Playgroud)

它会工作。