2017-03-17 99 views
0

我正在使用如果条件在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'); 
    } 

任何帮助 预先感谢

+4

''#abc''不是''abc''。 – zzzzBov

+0

它不工作的方式? – klutt

+0

当你得到第一个'alert(hash)'时你真的看到了什么 –

回答

4

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

var hash = window.location.hash; 

与此有关。 ::

var hash = window.location.hash.split('#')[1]; 

将完整代码::

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

它会工作。

+0

谢谢我在做错误取代:) –

+0

@DanishMalik如果它帮助你,请提出答案并接受它来帮助别人。 –

+0

是的,我做了,但他们没有让我在40秒之前做。再次感谢:)你们所有人。 –

0

window.location.hash将返回#abc而不是abc。您也正在删除#,但用' '而不是''替换它。试着做你的比较是这样的:

var hash = window.location.hash; 
alert(hash); /* Returns abc.*/ 
if(hash === '#abc'){ 
     alert(hash); /* not coming here.*/ 
    $('.tab').removeClass('is-active'); 
} 
+0

谢谢我在替换中犯了错误:) –

0

你用“”(空格)代替#..因此你真的有哈希是一个“ABC”,而不是“ABC” ......尝试下面代替

window.location.hash = "abc"; 
 
var hash = window.location.hash; 
 
var hash = hash.replace('#', ''); 
 
    alert(hash); /* Returns abc.*/ 
 
if(hash === 'abc'){ 
 
      alert(hash); /* now comes here*/ 
 
     //$('.tab').removeClass('is-active'); 
 
    }

0

替换 '',而不是用空格 '',它的工作。

var hash = '#abc'; 
 
var hash = hash.replace('#', ''); 
 
    alert(hash); /* Returns abc.*/ 
 
if(hash === 'abc'){ 
 
      alert(hash); /* not coming here.*/ 
 

 
    }