【问题标题】:If hash contains only 1 string, ignore other if statements如果 hash 仅包含 1 个字符串,则忽略其他 if 语句
【发布时间】:2017-10-11 04:16:48
【问题描述】:

所以我正在努力更新我的一个编码游乐场,但遇到了一些麻烦。

基本上,如果用户ONLY 在散列中有“结果”字符串,就像这样......

testhash.html#d81441bc3488494beef1ff548bbff6c2?result

希望它显示结果 ('only result was "result") 而没有其他内容,但如果用户在字符串中有编辑器,我希望它拆分并显示编辑器或在本例中文字...

testhash.html#d81441bc3488494beef1ff548bbff6c2?md,html,css,js,result

但是,我很难弄清楚如何过滤它,如果它只显示哈希中的结果字符串以忽略其他 if 语句。

如果函数只显示哈希中的“结果”字符串,我如何过滤到哪里让它忽略其他 if 语句而只显示结果?

var url = window.location.hash,
    testhash = "#d81441bc3488494beef1ff548bbff6c2",
    arr = ["md", "html", "css", "js", "edit", "dark", "transparent"];

$(document).ready(function(e) {
  if (!url) {
      window.location.href = location.href + testhash + "?md,html,css,js,result";
  } else {
    // Show Editors If URL Contains Them
    if (url.indexOf("?") > -1) {
      if (url.indexOf("result") > -1) {
        if ($.inArray("result", arr) > -1) {
          $(document.body).append('only result was "result" 1');
          return false;
        }

        if (url.indexOf("md") > -1) {
          $(document.body).append('md');
        }
        if (url.indexOf("html") > -1) {
          $(document.body).append('html');
        }
        if (url.indexOf("css") > -1) {
          $(document.body).append('css');
        }
        if (url.indexOf("js") > -1) {
          $(document.body).append('js');
        }
        if (url.indexOf("dark") > -1) {
          $(document.body).append('dark');
        }
        if (url.indexOf("transparent") > -1) {
          $(document.body).append('transparent');
        }

        $(document.body).append('result');
      }
    }
  }
});

【问题讨论】:

  • 如果它在哈希 url 中,您只想显示结果吗?而不是进入其他 if 语句,那么解决方案是将所有 if 语句包装在 if (url.indexOf("result") > -1) {} 的 else {} 中,就像这样 if (url.indexOf("? ") > -1) { if (url.indexOf("result") > -1) { ... } else { if (url.indexOf("md") > -1) { $(document.body).附加('md'); } if (url.indexOf("html") > -1) { $(document.body).append('html'); } ...} }
  • 如果显示“ONLY”结果,我想要文本only result was "result"。但是如果其他字符串在有或没有结果的情况下可见,我希望它附加文本,如代码示例中所示

标签: javascript jquery arrays if-statement hash


【解决方案1】:

我认为你想要的不是window.location.hash。相反,你想要的是window.location.search

使用给定的网址:

testhash.html#d81441bc3488494beef1ff548bbff6c2?md,html,css,js,result

当您使用window.location.search 时,您将得到?md,html,css,js,result 作为结果。然后就是

var url = window.location.search;
$(document).ready(function(e) {
  if (!url) {
      //do your code
  } 
  else 
  {
     if (url.length > 1)
     {
         var search = url.substr(1); // remove char ?
         if (search == 'result')  // means that the parameter is ONLY result
         {
              $(document.body).append('only result was "result"');
              return false;
         }
         else //other strings WITH or WITHOUT the "result"
         {
              if (search.indexOf("result") > -1) 
              { $(document.body).append('result'); }

              if (search.indexOf("md") > -1) 
              { $(document.body).append('md'); }

               // and the rest
         }
      }
    }
});

演示:https://jsfiddle.net/jxom6zhp/1/

【讨论】:

    猜你喜欢
    • 2018-11-25
    • 2020-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多