jQuery BBQ,将 HREF 与一组类深度链接 --- 可能吗?

发布于 2024-09-26 02:43:55 字数 283 浏览 9 评论 0原文

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

这样的 href 问题是它要求我在整个网站的所有 URL 中添加 #,这意味着非 - javascript 浏览器无法使用该网站...

我的问题是,有没有一种方法可以简单地向我想要深度链接的所有 HREF 添加一个类?像 class="deep-link-it-please" 这样的东西......想法?它还在 Rails 中造成了一个头痛,它使用 link_to ,它不支持在 URL 之前添加哈希 # ..谢谢

Currently, my understanding of jQuery BBQ is it requires me to use an href like href="#/stufff/123/13"

Problem with that is it requires me to add a # to all my URLs throughout the site which then means non-javascript browsers can't use the site...

My question, is there a way I can simply add a class to all HREFs I want deep linked? something like class="deep-link-it-please".... Ideas? It also creates a headache in Rails which uses link_to which does not support adding a hash # before the URL.. thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

靑春怀旧 2024-10-03 02:43:55

向您想要隐藏的链接添加哈希怎么样?

    $('.bbq-nav a').each(function(){
       var nice = $(this).attr('href');
       $(this).attr('href','#' + nice);
    });

What about adding a hash to the links you want to hijax?

    $('.bbq-nav a').each(function(){
       var nice = $(this).attr('href');
       $(this).attr('href','#' + nice);
    });
您的好友蓝忘机已上羡 2024-10-03 02:43:55

如果您打算劫持网址,则不需要将哈希添加到网址中。通过我的 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");

You shouldn't need to add the hash to your urls if you plan on hijacking them anyway. With normal links inside of my div#container and everything seems to be working fine using this implementation:

$("#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");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文