【问题标题】:Jquery on Hash Change and browser back button.Jquery on Hash Change 和浏览器后退按钮。
【发布时间】:2016-09-01 20:26:19
【问题描述】:

我有一系列隐藏内容的选项卡。我已经附加了 #1 #2 等的 url。换句话说,当有人单击一个选项卡时,它会将其添加到 url,然后使用 onhashaschanged。这是代码。

jQuery( ".slidelinks li" ).click(function() {


  var identify=jQuery( ".slidelinks li" ).index( this );
 window.location = "#" + identify ;

if ("onhashchange" in window) {
      jQuery( ".slidelinks li " ).addClass('active');

    jQuery( ".slidelinks li " ).removeClass('active');
jQuery(".slides").find('.dslc-modules-area').eq(identify).removeClass("hide");
    jQuery( ".slidelinks li " ).eq(identify).addClass('active');
}


});

我知道这与点击有关,但我试图弄清楚如何让标签在使用后退按钮或前进按钮时显示。我尝试在单击之外使用 onhaschange 来检测并查看后退按钮是否发生任何事情,但没有任何反应。我的问题是,如果使用从 mydomain.com/page/#3 回到 mydomain.com/page/#2 的后退按钮,我该如何运行 onhaschange 函数

【问题讨论】:

    标签: jquery browser window


    【解决方案1】:

    好吧,如果有人想知道,我认为它已经解决了。 我用过这个插件 https://github.com/cowboy/jquery-hashchange

    并更改为代码以具有以下内容。

    jQuery(function(){
    
      // Bind the event.
      jQuery(window).hashchange( function(){
    var locationminushas=location.hash.substring(1);
    
          jQuery( ".slidelinks li " ).addClass('active');
    
        jQuery( ".slidelinks li " ).removeClass('active');
    jQuery(".slides").find('.dslc-modules-area').addClass("hide");
    jQuery(".slides").find('.dslc-modules-area').eq(locationminushas).removeClass("hide");
        jQuery( ".slidelinks li " ).eq(locationminushas).addClass('active');
      })
    
      // Trigger the event (useful on page load).
      jQuery(window).hashchange();
    
    });
    

    【讨论】:

      猜你喜欢
      • 2012-03-22
      • 2018-08-10
      • 2017-06-29
      • 2010-09-14
      • 1970-01-01
      • 2013-03-02
      • 1970-01-01
      • 1970-01-01
      • 2011-02-17
      相关资源
      最近更新 更多