2013-03-16 45 views
3

我有jQuery页面的页面。每个标签都有自己的链接(mysite.com/index.html#tab1)。还有我的js功能在点击标签链接时起作用 - 它根据点击的标签改变页面上的一些内容。 问题: 我需要启用我的js功能,当访问者通过特定哈希链接进入站点时,而不仅仅是通过单击该选项卡。这意味着我需要检测URL中的哪个#并为链接中的特定#执行特定功能。散列检测和页面处理

回答

0

您可以使用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

displayme

+0

非常感谢您! – 2013-03-17 18:15:08