1

目前,我对 jQuery BBQ 的理解是它需要我使用 href="#/stufff/123/13" 这样的 href

问题在于它需要我在整个站点的所有 URL 中添加一个 #,这意味着非 JavaScript 浏览器无法使用该站点...

我的问题是,有没有一种方法可以简单地将一个类添加到我想要深度链接的所有 HREF 中?类似于 class="deep-link-it-please".... 的想法?它还在使用不支持在 URL 之前添加哈希 # 的 Rails 中创建了一个令人头疼的问题。谢谢

4

2 回答 2

1

如果您打算劫持它们,则不需要将哈希添加到您的 url。在我的 div#container 中使用正常链接,使用此实现似乎一切正常:

$("#container a").live('click', function(){
    var hrefRequested = $(this).attr( "href" );

    // Push this URL "state" onto the history hash.
    $.bbq.pushState({ url: hrefRequested });

    // Prevent the default click behavior.
    return false;
});

// Bind a callback that executes when document.location.hash changes.
$(window).bind( "hashchange", function(e) {
    var url = $.bbq.getState( "url" );

    // Used to make sure that when you back up to the page you came in on
    // you don't get an empty page.
    if ( url == undefined ) { url = window.location.href; }

    $('#content-container').load(url + ' #container', function(response, status, xhr) {
        if (status == "success") {
            console.log('hooray!');

            // document.title = ;
            // document.location.href = hrefRequested;

            // return false;
        }

        if (status == "error") {
            var msg = "Sorry but there was an error: ";
            $("#error").html(msg + xhr.status + " " + xhr.statusText);
        }
    });

// You probably want to actually do something useful here..
});

// Since the event is only triggered when the hash changes, we need
// to trigger the event now, to handle the hash the page may have
// loaded with.
$(window).trigger("hashchange");
于 2012-01-05T18:26:45.827 回答
1

将哈希添加到您想要 hijax 的链接怎么样?

    $('.bbq-nav a').each(function(){
       var nice = $(this).attr('href');
       $(this).attr('href','#' + nice);
    });
于 2011-01-12T20:57:34.693 回答