2010-12-19 57 views
17
jQuery(document).ready(function(){ 
    $("#red-products").hide(); 
    $("#content-info").click(function(event){ 
     $("#red-products").hide(); 
     $("#red-information").show(); 
    }); 

    $("#content-product").click(function(event){ 
     $("#red-information").hide(); 
     $("#red-products").show(); 
    }); 

    $("#more").click(function(event){ 
     load(this.href); 
     return false; 
    }); 

}); 

如您所见,默认情况下,#red-products已隐藏,#red-information可见。有时候我想#red-products可见和#red-information隐藏的,这意味着像使用jQuery获取#anchor的URL?

http://localhost/networks2/profile.php?id=1&offset=1#products

显示#red-products和隐藏#red-information。而

http://localhost/networks2/profile.php?id=1&offset=1#information

隐藏#red-products并显示#red-information

如何使用jQuery从URL读取锚点并隐藏/显示适当的部分?

回答

36

您可以更改初始隐藏是基于window.location.hash,通过更换此:

$("#red-products").hide(); 

有了这个:

$("#red-products, #red-information").hide(); 
$("#red-" + (window.location.hash.replace("#", "") || "information")).show(); 

这将隐藏两个最初,然后显示hasd(#red-hashhere) ,或者默认为现在显示#red-information

+0

我不知道它不起作用:| – 2010-12-19 14:25:29

+0

@亚当 - 这不是一个非常有用的描述:)什么是不工作......你是否正确地使用网址,就像你在问题中一样? – 2010-12-19 14:27:28

+0

ups位置mising h:D非常感谢matee! – 2010-12-19 14:28:12