javascript 如果没有散列,则 jquery 散列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4686579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 14:32:23  来源:igfitidea点击:

jquery hash if no hash

javascriptjqueryhash

提问by Thomas

Trying to get #home to display if there is no hash present in the url. I figured something along these lines would work pretty easy but I can't get anything going:

如果 url 中不存在哈希,则尝试让 #home 显示。我认为沿着这些路线做的事情会很容易,但我什么也做不了:

   if(window.location.hash != null){ 
      $(window.location.hash).fadeIn(800);
   } else {
      $('#home').fadeIn(800);
   }

I've never worked with if / else statements in jquery, so this is obviously wrong

我从来没有在 jquery 中使用过 if / else 语句,所以这显然是错误的

thanks!

谢谢!

回答by BoltClock

Compare it against the empty string instead (null and the empty string aren't equal in JavaScript):

将其与空字符串进行比较(在 JavaScript 中 null 和空字符串不相等):

if(window.location.hash != ''){ 
   $(window.location.hash).fadeIn(800);
} else {
   $('#home').fadeIn(800);
}