使用jQuery获取URL的#anchor? [英] Get #anchor for URL using jQuery ?

查看:97
本文介绍了使用jQuery获取URL的#anchor?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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隐藏,这意味着类似

As you can see, by default #red-products is hidden and #red-information is visible. Sometimes I want #red-products to be visible and #red-information hidden, meaning something like

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读取锚,并隐藏/显示适当的部分?

How can I read anchor from URL using jQuery, and hide/show appropriate sections?

推荐答案

您可以将初始隐藏更改为基于 window.location.hash ,方法是替换为:

You can change the initial hide to be based on window.location.hash, by replacing this:

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

与此:

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

这将首先隐藏这两个内容,然后显示hasd(#red-hashhere),或者默认为像现在一样显示#red-information.

This will hide both initially, then show the hasd (#red-hashhere), or default to showing #red-information as you have now.

这篇关于使用jQuery获取URL的#anchor?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆