【问题标题】:Hash detection and page processing哈希检测和页面处理
【发布时间】:2013-03-17 09:19:32
【问题描述】:

我有带有 jquery 选项卡的页面。每个选项卡都有自己的链接 (mysite.com/index.html#tab1)。还有我的 js 函数在单击选项卡链接时起作用 - 它根据单击的选项卡更改页面上的一些内容。 问题: 当访问者通过具有特定哈希的链接进入网站时,我需要启用我的 js 功能,而不仅仅是通过单击选项卡。这意味着我需要检测 URL 中的哪个 # 并针对链接中的特定 # 执行特定功能。

【问题讨论】:

    标签: hash tabs hyperlink


    【解决方案1】:

    您可以使用 javascript 并检查 url 是否包含任何 # 并获取 # 是什么,然后适当地更改页面。

    // get the current url.
    var URL = window.location;
    // URL = 'http://example.net/#displayme'; // just an example that will work.
    var hashtag = 'null';
    
    try{
        // get a substring of everything after the # excluding the '#'
        hashtag = URL.substr(URL.indexOf('#') + 1, URL.length); 
    
        if(hashtag == 'someVar'){
             // change content
        }
    } catch(err){
        //the url didn't contain a #
    }
    
    document.write(hashtag);
    

    上面的脚本基本上是检查 URL 并尝试查找它是否包含任何 #,您可以将脚本放在 $(document).ready(function() { addScriptHere(); }); 函数中,以便在页面加载后调用它,然后使用 jQuery 更改页面中的任何一个随心所欲。


    上例中使用注释掉的 URL 输出

    显示我

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-05
      • 1970-01-01
      • 2015-11-24
      • 2014-01-04
      • 2012-12-10
      • 1970-01-01
      • 1970-01-01
      • 2019-03-20
      相关资源
      最近更新 更多