我试图在提交表单或刷新页面后保持当前选项卡打开,但无法打开。
我已经看到了关于这个问题的答案,但我的情况有点不同,因为我在选项卡上没有使用ul和li,甚至没有使用href。我用了按钮。以下是我的代码:
HTML
<div class="tab">
<button class="tablinks colora" onclick="tabsetting(event, 'London')" id="defaultOpen">General settings</button>
<button class="tablinks color1" onclick="tabsetting(event, 'Paris')">Attendance sheet settings</button>
<button class="tablinks color0" onclick="tabsetting(event, 'Tokyo')">Certificate settings</button>
<button class="tablinks color01" onclick="tabsetting(event, 'Douala')">Certificate templates</button>
</div>
<div id="London" class="tabcontent">
</div>
Javascript
function tabsetting(evt, cityName) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
document.getElementById(cityName).style.display = "block";
evt.currentTarget.className += " active";
}
// Get the element with id="defaultOpen" and click on it
document.getElementById("defaultOpen").click();
在页面刷新或表单提交后,如何使其中一个选项卡中的当前选项卡保持打开状态?
嗨,我找到了问题的解决方案。我把它粘贴在这里供任何人学习:
$('.tablinks').click(function(){
var activetab = $(this).attr('id');
localStorage.setItem('activetab', activetab );
});
$(function() { //short hand for $(document).ready()
var activetab = localStorage.getItem('activetab');
document.getElementById(activetab).click();
});