2017-03-17 7 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

最初の '警告(ハッシュ)'を受け取ったときに実際に表示されるもの –

答えて

4

window.location.hash#abcないabcを返しますので、これによりコード::

var hash = window.location.hash; 

の下に取り付けます。 ::

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

フルコードは::

となります。

+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」である...してみてください

0

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'); 
 
    }

代わりに下に 'で' といないスペースで ''、その作業交換してください。

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

 
    }

0

現在のURLがhttp://www.example.com/yourfile.htm#abc

var hash = window.location.hash; // hash = #abc 
if(hash === '#abc'){ 
    //You are in now 
    $('.tab').removeClass('is-active'); 
} 

であると仮定し、この意志がお役に立てば幸いです。

関連する問題