我正在尝试获取 anchor 链接以打开特定页面上的选项卡。
类似 https://jonathanbossenger.com/using-anchor-links-to-open-accordions-and-tabs-in-divi/ 上找到的解决方案仅当 anchor 链接与选项卡位于同一页面时才有效。
我在 Stack 上发现了一个不同的线程来解决这个问题,但它在我无权访问的聊天中解决了最终的解决方案 ( Wordpress Divi Theme - Anchor link opens tab toggle )。
我可以在他们的网站上看到他们能够让它工作(https://www.elkodowntown.org/our-members/#member-tabs|3)。
我如何访问 Elko Downtown 的站点代码以找到下面使其正常工作的 JavaScript 最终版本?
jQuery(function($) {
$('.menu-item-179 a').on('click', function(event){
tabScroll('.et_pb_tab_0');
return false;
});
$('.menu-item-180 a').on('click', function(event){
tabScroll('.et_pb_tab_1');
return false;
});
$('.menu-item-181 a').on('click', function(event){
tabScroll('.et_pb_tab_2');
return false;
});
$('.menu-item-182 a').on('click', function(event){
tabScroll('.et_pb_tab_3');
return false;
});
$('.menu-item-183 a').on('click', function(event){
tabScroll('.et_pb_tab_4');
return false;
});
function tabscroll(target) {
$("html, body").animate({ scrollTop: $('#member-tabs').offset().top }, 1000);
setTimeout($('#member-tabs' + target + ' a').click(), 2000 );
}
$(function hash() {
var hash = window.location.hash.replace('#', "");
if (hash == '#shop') { tabscroll('.et_pb_tab_1'); }
if (hash == '#service') { tabscroll('.et_pb_tab_0'); }
if (hash == '#eat-drink') { tabscroll('.et_pb_tab_2'); }
if (hash == '#arts-entertainment') { tabscroll('.et_pb_tab_3'); }
if (hash == '#stay') { tabscroll('.et_pb_tab_4'); }
});
});
最佳答案
通过检查浏览器可以看到通过网络传输的每一行 javascript。如果您只想查看他们最终确定的代码,请查看下面的内容。
但是,如果您正在使用 WP 并尝试使用 JS/JQuery 执行操作,我强烈建议您首先学习如何执行 WP 之外的功能并了解发生了什么,您从中获取的代码并不总是匹配您的页面结构/元素,您总是想知道为什么事情不起作用。
这是为他们执行此操作的代码:
function _setTab(){
// get current hash value
var curHash = window.location.hash.substr(1);
// only continue if hash provided and scoped to member tabs
if( !curHash || !curHash.match('member-tabs') ){ return false; }
// split and int val tab num
curHash = parseInt(curHash.split('|')[1]);
// ignore if tab is current state
if( curHash === window._tabSelected ){ return false; }
// set current tab to window
window._tabSelected = curHash;
// add click event to tab selected
switch(curHash){
case 0:
case 1:
case 2:
case 3:
case 4:
jQuery('#member-tabs .et_pb_tab_'+curHash+' a').click();
break;
default:
return false;
break;
}
// scroll to tabs container
_scrollToTabs();
}
// scroll to member tabs container with 50px offset
function _scrollToTabs(){
var oTabs = jQuery('#member-tabs');
if( oTabs.length > 0 ){
jQuery('html,body').animate({
scrollTop: (oTabs.offset().top - 50)
}, 1000);
}
return false;
}
// set falsey state for tab selected on load
window._tabSelected = false;
// we need to attach to window load because the tabs functions are initialized later in document
jQuery(window).on('load', function(){
// check for initial hash state
_setTab();
// add hash change window listener
jQuery(window).on('hashchange', function(){
_setTab()
});
// void submenu when we are in member section
var curPath = window.location.pathname;
if( curPath.match('our-members') ){
// only change hash and do not reload page
jQuery('#menu-item-98 ul li a').on('click', function(e){
e.stopImmediatePropagation();
window.location.hash = jQuery(this).prop('hash');
return false;
});
}
});
关于javascript - WordPress Divi 主题 - anchor 链接打开特定选项卡切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60496757/