我们正在使用下面的代码来链接到我们网站上的特定选项卡。
目前,它允许我们通过在URL上添加#member-tabs | 3链接到特定选项卡,其中member-tabs是类,3是要打开的选项卡,并用“ |”。
问题:符号“ |”被编码为“%7C”,但未能在电话/平板电脑上解码。我尝试添加replace('%7C', '|')
,但无法解决问题。
示例:https://test.radacutlery.com/fundraising/tabs/#member-tabs|3
<script>
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.replace('%7C', '|')split('|')[1]);
// testing the decodeURI
decodeURI(curHash)
// 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;
// attach to window load because the tabs 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()
});
});
</script>
不确定这是否是问题,但我在此行上看到语法错误:
// split and int val tab num
curHash = parseInt(curHash.replace('%7C', '|')split('|')[1]);
在split
之前应有一个句点,如下所示:
// split and int val tab num
curHash = parseInt(curHash.replace('%7C', '|').split('|')[1]);