jQuery BBQ:在哪里存储URL? [英] JQuery BBQ: Where to store URLs?

查看:116
本文介绍了jQuery BBQ:在哪里存储URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

链接到演示页面: http://benalman.com/代码/项目/jquery-bbq/examples/fragment-advanced/

脚本:

<script type="text/javascript" language="javascript">

$(function(){

  // For each .bbq widget, keep a data object containing a mapping of
  // url-to-container for caching purposes.
  $('.bbq').each(function(){
    $(this).data( 'bbq', {
      cache: {
        // If url is '' (no fragment), display this div's content.
        '': $(this).find('.bbq-default')
      }
    });
  });

  // For all links inside a .bbq widget, push the appropriate state onto the
  // history when clicked.
  $('.bbq a[href^=#]').live( 'click', function(e){
    var state = {},

      // Get the id of this .bbq widget.
      id = $(this).closest( '.bbq' ).attr( 'id' ),

      // Get the url from the link's href attribute, stripping any leading #.
      url = $(this).attr( 'href' ).replace( /^#/, '' );

    // Set the state!
    state[ id ] = url;
    $.bbq.pushState( state );

    // And finally, prevent the default link click behavior by returning false.
    return false;
  });

  // Bind an event to window.onhashchange that, when the history state changes,
  // iterates over all .bbq widgets, getting their appropriate url from the
  // current state. If that .bbq widget's url has changed, display either our
  // cached content or fetch new content to be displayed.
  $(window).bind( 'hashchange', function(e) {

    // Iterate over all .bbq widgets.
    $('.bbq').each(function(){
      var that = $(this),

        // Get the stored data for this .bbq widget.
        data = that.data( 'bbq' ),

        // Get the url for this .bbq widget from the hash, based on the
        // appropriate id property. In jQuery 1.4, you should use e.getState()
        // instead of $.bbq.getState().
        url = $.bbq.getState( that.attr( 'id' ) ) || '';

      // If the url hasn't changed, do nothing and skip to the next .bbq widget.
      if ( data.url === url ) { return; }

      // Store the url for the next time around.
      data.url = url;

      // Remove .bbq-current class from any previously "current" link(s).
      that.find( 'a.bbq-current' ).removeClass( 'bbq-current' );

      // Hide any visible ajax content.
      that.find( '.bbq-content' ).children( ':visible' ).hide();

      // Add .bbq-current class to "current" nav link(s), only if url isn't empty.
      url && that.find( 'a[href="#' + url + '"]' ).addClass( 'bbq-current' );

      if ( data.cache[ url ] ) {
        // Since the widget is already in the cache, it doesn't need to be
        // created, so instead of creating it again, let's just show it!
        data.cache[ url ].show();

      } else {
        // Show "loading" content while AJAX content loads.
        that.find( '.bbq-loading' ).show();

        // Create container for this url's content and store a reference to it in
        // the cache.
        data.cache[ url ] = $( '<div class="bbq-item"/>' )

          // Append the content container to the parent container.
          .appendTo( that.find( '.bbq-content' ) )

          // Load external content via AJAX. Note that in order to keep this
          // example streamlined, only the content in .infobox is shown. You'll
          // want to change this based on your needs.
          .load( url, function(){
            // Content loaded, hide "loading" content.
            that.find( '.bbq-loading' ).hide();
          });
      }
    });
  })

  // 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' );

});

$(function(){

  // Syntax highlighter.
  SyntaxHighlighter.highlight();

});

</script>

身体:

<div class="bbq" id="bbq1">
  <div class="bbq-nav bbq-nav-top">
    <a href="#burger.html">Burgers</a> |
    <a href="#chicken.html">Chicken</a> |
    <a href="#kebabs.html">Kebabs</a>
  </div>

那么,这里的问题是我保存自己的链接的地方?我的意思是单击链接后在DIV中加载的那些内容.

So, here my problem is where i save my own links? i mean those contents which loads in DIV after clicking on a Link.

内容示例:"JQUERY BBQ 单击上面的导航项以加载一些美味的AJAX内容!同样,一旦加载了内容,请单击您可能会看到的任何内联链接,以进一步探索我们的美味佳肴. &图片"

One Example of Contents: "JQUERY BBQ Click a nav item above to load some delicious AJAX content! Also, once the content loads, feel free to further explore our savory delights by clicking any inline links you might see. & A Pic"

推荐答案

您的旧网址存储在浏览器历史记录中.浏览器会记住您去过的所有哈希位置.

Your old urls are stored in the browser history. The browser remember all the hash places you've been.

如果我转到a.com 然后去a.com#12 然后转到a.com#1394 然后我可以在浏览器中单击返回到 a.com#12,然后我可以再次单击以返回到a.com

if I go to a.com then go to a.com#12 then go to a.com#1394 and then I can click back in my browser and it goes back to a.com#12 then I can click back again and it goes back to a.com

我认为bbq将所有状态信息存储在url中.那就是$ .bbq.pushState所做的 让我们看一下我最近编写的一些代码.

I think bbq stores all the state infomation in the url. Thats what the $.bbq.pushState does Lets take a look at some code i wrote recently.

          $(function () {
            $("a[data-custom-ajax-link='true']").click(function (e) {
                var target = $(this).data('target');
                var url = $(this).attr('href');

                $.bbq.pushState({ url: url, target: target});

                e.preventDefault();
            });

当某人单击以下形式的网址

When somebody clicks on a url of the form

 <a href="somelink.html" data-custom-ajax-link="true" data-target='#TargetId'>Link </a>

bbq.pushState事件触发.此bbq.pushstate以输入到bbq.pushState方法中的参数的形式将哈希值附加到url上.即.

the bbq.pushState event fires. This bbq.pushstate appends a hash onto the url in the form of the parameters fed into the bbq.pushState method. ie.

  oldurl#url=somelink.html&target=#TargetID

(除了&和=#都是url编码的)

(except the & and the =# are url encoded)

您绑定到"hashchange"窗口事件以捕获这些散列,这些散列在URL末尾滚动或从URL末尾退回(当您单击链接并且哈希改变时,或者当您单击后退按钮并且哈希变回时)

you bind to the "hashchange" window event to catch these hashes as they roll onto or back off the end of the url (when you click a link and the hash changes or when you the back button and the hash changes back)

   $(window).bind("hashchange", function(e) {
         var url = $.bbq.getState("url");
         var $target = $($.bbq.getState("target"));
         //fire ajax here or switch tabs etc.
    });

这篇关于jQuery BBQ:在哪里存储URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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