如果语句逻辑没有产生所需的结果

时间:2016-05-20 15:52:30

标签: javascript jquery html if-statement

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

对于各个服务链接,它们看起来像这样:

service-display-box

这个系统的这个方面有效。它将我带到服务页面,只显示我点击的链接。

无效的部分未显示服务或导航菜单中的//added。现在所有的服务都显示出来,而service-display-box也是如此。

我试图创建逻辑来防止这种情况发生,但它无法正常工作。在下面的javascript中,您将看到service-display-box之类的评论,这是我添加的尝试并创建逻辑,如果浏览器中有哈希值,它将显示$(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> 和特定服务项,或者它根本不会显示if (sectionName > 1) {

我的逻辑出了什么问题?

C:\extras\code\gitlab\project-name>ng lint

> Project-Name@0.4.0 lint C:\extras\code\gitlab\project-name
> tslint "src/**/*.ts"


Lint errors found in the listed files.

我也试过

{{1}}

1 个答案:

答案 0 :(得分:3)

window.location.hash.slice(1)返回一个空字符串,与null不同。我想你想检查一下.length == 0

相关问题