Select an anchor within a JQUERY Tab from a link on anther webpage

只愿长相守 提交于 2019-12-04 14:44:06
Dan Gribble

I managed to get it working myself by supplying both the tab name and anchor name in the URL, and then parsing it with javascript.

Following that, I then passed the tab number to jquery to open the tab and did a simple animated scroll to the anchor.

e.g. URL is http://www mysite com#tab-3&#myanchor javascript is

$(document).ready(function(){
  var hash_parts = location.hash.split('&', 2); //2 - limit, may be changed if more than two arguments.
  var tab        = hash_parts[0];               // Tab number part of url.  Array starts at 0 for 1st element.
  var anc        = hash_parts[1];               // Anchor name.
  var tabId      = tab.split("-").pop()-1;      // Tab no. relating to Jquery ui index no. (starts at zero for tab 1.)  

  $("#tabs").tabs("option", "active", tabId);  // Select the tab.
  $('html, body').animate({'scrollTop': $(anc).offset().top}, 1000); // Animated scroll to anchor.
});

This is using JQuery UI 1.10.3 and JQuery 1.7.2

TO address an issue using a mobile device, sometimes it will turn the second # into %23.

To fix the error, you would add this line above the last line:

  if(anc !== ''){
    anc = anc.replace('%23', '#');
  }

I've found this, can this be helpful for you?

http://raw10.cust.magicedit.com/sandbox/jqueryTabs/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!