1

链接到演示页面:http ://benalman.com/code/projects/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 中的内容。

内容示例:“JQUERY BBQ 单击上方的导航项目以加载一些美味的 AJAX 内容!此外,一旦内容加载,请随时通过单击您可能看到的任何内联链接进一步探索我们的美味佳肴。& A Pic”

4

1 回答 1

2

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

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

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

          $(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();
            });

当有人点击表单的 url

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

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

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

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

您绑定到“hashchange”窗口事件以捕获这些哈希值,因为它们滚动到或退出 url 的末尾(当您单击链接并且哈希值更改或当您返回按钮并且哈希值变回时)

   $(window).bind("hashchange", function(e) {
         var url = $.bbq.getState("url");
         var $target = $($.bbq.getState("target"));
         //fire ajax here or switch tabs etc.
    });
于 2013-06-14T00:39:09.153 回答