让Ben Alman的代码适用于我的网站

时间:2012-02-21 09:19:25

标签: jquery hash hashchange

我正在使用一个名为pages的ajax并尝试使浏览器的“后退”和“前进”按钮起作用。 我试图使用ben alman的插件,因为它似乎最受欢迎,但此时对我不起作用。我想知道问题所在。我非常害羞,这是我不理解的代码中的简单内容,所以请帮忙。我会告诉你每个代码:):

这些是处理程序:

<li><a href="#" class="ceni">примерни цени</a></li>
<li><a href="#" class="karieri">кариери</a></li> 

这是我的javascript调用页面而不重新加载整个页面(ajax):

$('.ceni').click(function(){             
    $.ajax({
    url: 'pages/ceni.php',
    success: function(response) {
    $("#pages").html(response);
    }
     });    
  });



  $('.karieri').click(function(){           
    $.ajax({
    url: 'pages/karieri.php',
    success: function(response) {
    $("#pages").html(response);
    }
     });        
  });

这些是本阿尔曼的代码:

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

$(function(){

  // Keep a mapping of url-to-container for caching purposes.
  var cache = {
    // If url is '' (no fragment), display this div's content.
    '': $('.bbq-default')
  };

  // Bind an event to window.onhashchange that, when the history state changes,
  // gets the url from the hash and displays either our cached content or fetches
  // new content to be displayed.
  $(window).bind( 'hashchange', function(e) {

    // Get the hash (fragment) as a string, with any leading # removed. Note that
    // in jQuery 1.4, you should use e.fragment instead of $.param.fragment().
    var url = $.param.fragment();

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

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

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

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

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

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

        // Append the content container to the parent container.
        .appendTo( '.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.
          $( '.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>

注意:我不是专家或其他什么,所以请尽可能地跟我说说。非常感谢:)

1 个答案:

答案 0 :(得分:1)

BBQ插件不会神奇地使后退和前进按钮工作。 BBQ插件只是更容易更改浏览器哈希(并且还支持多个哈希值)。您仍然需要自己更改哈希值。

在这里阅读更多关于哈希的内容......

https://developers.google.com/tv/web/articles/location-hash-navigation