如果url哈希不包含任何字符串,请执行功能

本393

我正在编写一个脚本,以根据哈希中的字符串更改可见的元素。该脚本将进入博客页面,用户可以在其中按类别过滤博客文章。当用户过滤到某个类别时,该类别将以哈希链接的形式附加到URL。

example.com/#categoryA

到那时,我希望与每个已过滤帖子集相对应的文本框出现在投资组合的旁边。但是,如果用户尚未过滤帖子,或者从过滤帖子转到查看所有帖子,则网址中只有一个哈希,如下所示:

example.com/#

在这种情况下,我希望显示一个默认文本框。

我的脚本(如下所示)执行所有操作,但当URL仅以哈希结尾时,将显示默认文本框。

var frag = window.location.href.split("#");
var hashClassChange = function() {
  if (window.location.hash) {
    //If the url hash contains commercial, show the commercial box, hide the last active box
    if (window.location.hash.indexOf('commercial') == 1) {
      $('#box1').addClass("active");
      $('#default').removeClass("active");
      $('#box2').removeClass("active");
      $('#box3').removeClass("active");
      $('#box4').removeClass("active");
    } 
    //If the url hash contains hospitality, show the hospitality box, hide the last active box
    else if (window.location.hash.indexOf('hospitality') == 1) {
      $('#box2').addClass("active");
      $('#default').removeClass("active");;
      $('#box1').removeClass("active");
      $('#box3').removeClass("active");
      $('#box4').removeClass("active");
    } 
    //If the url hash contains municipal-institutional, show the municipal / institutional box, hide the last active box
    else if (window.location.hash.indexOf('municipal-institutional') == 1) {
      $('#box3').addClass("active");
      $('#default').removeClass("active");
      $('#box1').removeClass("active");
      $('#box2').removeClass("active");
      $('#box4').removeClass("active");
    } 
    //If the url hash contains residential, show the residential box, hide the last active box
    else if (window.location.hash.indexOf('residential') == 1) {
      $('#box4').addClass("active");
      $('#default').removeClass("active");
      $('#box1').removeClass("active");
      $('#box2').removeClass("active");
      $('#box3').removeClass("active");
    } 
    //If the url hash does not contain any string, show the default box
    else if (!frag[1].length) {
      $('#default').addClass("active");
      $('#box1').removeClass("active");
      $('#box2').removeClass("active");
      $('#box3').removeClass("active");
      $('#box3').removeClass("active");
    }
  }
};
// repeats function every 500 milliseconds to check if the url hash has been changed
setInterval(hashClassChange, 500);

如何修复此脚本,以便在URL以单个哈希结束时显示默认文本框?

我已经设置了一个代码笔(http://codepen.io/ben393/pen/GNVqRX)来演示此操作。

男子

不知道为什么要使用这个frag变量,它永远都不会改变。

您可以执行以下操作

if(window.location.hash.length < 2) { ..... }

另外,如果没有什么阻止您,则可以使用该onhashchange事件,而不是通过以下方式连续检查setInterval

$(window).bind('hashchange', hashClassChange);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如果url不包含数组中的字符串,则执行函数

如果WebView网址包含字符串,请执行某些操作

如果字符串包含0或负值,请更改文本

如果LINQ中包含空字符串,LINQ应该不返回任何记录

确定字符串中是否包含不在字符列表中的任何字符,如果是,则哪些字符不匹配?

如果不包含某些字符串,请替换某些Child值?还是重写XPATH查询?网站抓取

如果 url 不包含子字符串,如何将路径添加到 url

如果element属性在特定数组中不包含任何内容,请执行某些操作

PowerShell-如果文件中不包含字符串,则继续(检测cmdlet何时不返回任何内容)

如果URL不包含特定字符串,则HTACCESS重定向

如果url不包含某些字符串,则htaccess重定向

如果任何列包含特定字符串,则删除行

如果任何行包含子字符串,则标记

如果字符串不包含和REGEX

Java- Regex用于不包含后跟任何字符的子字符串的字符串

检查字符串是否包含哈希中的任何键,并返回其包含的键的值

如果URL包含字符串A但不包含字符串B,则重定向

如果字符串包含某个子字符串,则任何字符串分配上的断点

如果文本字符串存在,请选择包含文本字符串的单元格

如果熊猫数据框中包含特定的子字符串,请替换该字符串

如果另一列包含字符串,请替换熊猫中的一列

jQuery如果选项文本包含字符串,请选择它

如果字符串中包含特定单词,请删除该单词

如果包含某些字符串MS-Access 2016,请删除整个单元格

如果列包含特定值,请选择预定义的字符串

如果字符串包含其他(如果不包含)-Javascript

如果字符串在大写和小写中包含相同的字符,如何在哈希集中添加字符串值?

如果标头行不包含子字符串且范围不包含#N / A,则求和

Ruby-如果实例不匹配,请替换字符串中的单词