使用attr('id')的hashchange,但是使用jquery从attr('href')加载内容

时间:2011-01-28 11:11:08

标签: jquery hash

嘿伙计们,我不确定它是否可以完成,但我只是好奇。

这是我的代码,我想知道它是否可以在理论上起作用......

//------------------------------------------------------------------
// Menu links
//------------------------------------------------------------------
var newHash      = "",
uri = "",
$content = $("#main-content"),
content  = '#main-content',
$wrap    = $("#wrap"),
baseHeight   = 0,
$el;

$wrap.height($wrap.height());
baseHeight = $wrap.height() - $content.height();

$("nav a").live('click', function(){
    window.location.hash = $(this).attr("id");
    url = $(this).attr("href");
    uri = windows.location.hash;
    return false;
});
/*$("nav").delegate("a", "click", function() {
    window.location.hash = $(this).attr("href");
    console.log(window.location.hash);
    return false;
});*/

$(window).bind('hashchange', function(){
    //window.location.hash = $(this).attr("href");
    //newHash = window.location.hash.substring(1);
    u = uri.substring(1);
    console.log(u);
    if (window.location.hash = u) {
        $wrap.find("#main-content").fadeOut(200, function() {
            $wrap.load(u + " #main-content", function() {
                $content.fadeIn(function() {
                    $wrap.animate({
                        height: baseHeight + $content.height() + "px"
                    }, 500);
                });
            });
        });
    }
});
$(window).trigger('hashchange');

目前的行为:
- 当我加载www.abc.com/时,URI自动变为www.abc.com/# - 当我点击菜单链接时,它会将URI转换为www.abc.com/#!/files/2
(其中 2 是用户的ID,!/ files / 2 是链接的ID)
- 当快速调用click时,将URI转换为www.abc.com/file.php# 现在只是直接从服务器加载文件而没有从页面加载内容而不是直接链接的ajax效果。

我已经成功地获得了显示为的链接:

http://www.abc.com/#!/file/2并从file.php加载内容但是由于成功,我无法欺骗浏览器查看URI是什么,并根据页面上的URI从file.php加载内容刷新(用于书签原因)

1 个答案:

答案 0 :(得分:0)

据我所知,触发浏览器中的URL更改(散列更改除外)加载新页面(在本例中为重新加载)。因此,当调用#!/ file / 2时,不应该在状态栏中显示“file.php”...

相关问题