Preventing scroll when using URI hash to identify tab

前端 未结 8 1837
陌清茗
陌清茗 2020-12-11 16:05

I\'m using JQuery UI to make tabs in my application. I needed the tabs to be linkable (direct link that opens the page and selects the correct tab). This is done by using a

8条回答
  •  [愿得一人]
    2020-12-11 16:44

    As others have mentioned the code from @Mottie might have once worked on older versions of jQuery UI, but this has definitely stopped working. The jQuery UI Tabs api has changed quite a bit since that was written so here is an updated version that works with at least jQuery 1.10.2

    Demo here: http://jsfiddle.net/xsx5u5g2/

    var $tabs = $("#tabs");
    $tabs.tabs({
      create: function(event, ui) {
        // Adjust hashes to not affect URL when clicked.
        var widget = $tabs.data("uiTabs");
        widget.panels.each(function(i){
          this.id = "uiTab_" + this.id; // Prepend a custom string to tab id.
          widget.anchors[i].hash = "#" + this.id;
          $(widget.tabs[i]).attr("aria-controls", this.id);
        });
      },
      activate: function(event, ui) {
        // Add the original "clean" tab id to the URL hash.
        window.location.hash = ui.newPanel.attr("id").replace("uiTab_", "");
      },
    });
    

提交回复
热议问题