Javascript 如果语句逻辑不能产生期望的结果

Javascript 如果语句逻辑不能产生期望的结果,javascript,jquery,html,if-statement,Javascript,Jquery,Html,If Statement,我有一个叫做“服务”的页面。当您从导航菜单转到该页面时,我不希望显示任何服务(太复杂,无法解释)。然后在我的索引页上,我有每种服务类型的链接。如果您单击一个链接,它将带您进入服务页面,并仅显示该服务 对于单个服务链接,它们如下所示: <a href="serviceCheck2#service1">Service 1</a> <a href="serviceCheck2#service2">Service 2</a> <a href="ser

我有一个叫做“服务”的页面。当您从导航菜单转到该页面时,我不希望显示任何服务(太复杂,无法解释)。然后在我的索引页上,我有每种服务类型的链接。如果您单击一个链接,它将带您进入服务页面,并仅显示该服务

对于单个服务链接,它们如下所示:

<a href="serviceCheck2#service1">Service 1</a>
<a href="serviceCheck2#service2">Service 2</a>
<a href="serviceCheck2#service3">Service 3</a>
我也试过了


if(sectionName>1){
window.location.hash.slice(1)
返回一个空字符串,它与null不同。我想您应该改为检查
.length==0

window.location.hash.slice(1)
返回一个空字符串,它与null不同。我想您应该改为检查
。length==0

您使用的是php吗?如果您使用php@C.oast不,我使用的是javascript。您使用的是php吗?如果您使用的是php@C.oast不,我在用javas谢谢!我改变了if和else的顺序,但它成功了。再次感谢!!谢谢!我改变了if和else的顺序,但它成功了。再次感谢!!
$(function(){

    //get the section name from hash
    var sectionName = window.location.hash.slice(1);

    if (sectionName != null) { //added
    //then show the section
     $('#service-display-box').show();
     $(window.location.hash).show().scroll().siblings().hide();
    } else { //added
        $('#service-display-box').hide(); //added
    } //added
})

#service-display-box {
    display: none;
}
<div id="service-display-box">
      <div id="service-display-box-container">
        <div class="service-item-box" id="service1">
          <div class="service-item-title">1</div>
          <h2 class="service-item-description"> Service</div>
        </h2>
        <h2 class="service-item-box" id="service2">
          <div class="service-item-title">2</div>
          <div class="service-item-description"> Service</div>
        </h2>
        <h2 class="service-item-box" id="service3">
          <div class="service-item-title">3</div>
          <div class="service-item-description"> Service</div>
        </h2>
        <h2 class="service-item-box" id="service4">
          <div class="service-item-title">4</div>
          <div class="service-item-description"> Service</div>
        </h2>
        <h2 class="service-item-box" id="service5">
          <div class="service-item-title">5</div>
          <div class="service-item-description"> Service/div>
        </h2>
        <h2 class="service-item-box" id="service6">
          <div class="service-item-title">6</div>
          <div class="service-item-description"> Service</div>
        </h2>
        <div style="clear:both;"></div>
        <div id="service-top"><span id="service-top-border">Back to all services</span></div>
      </div>
    </div>