tabs.js 805 B

123456789101112131415161718192021
  1. document.addEventListener('DOMContentLoaded', function() {
  2. var tabs = document.getElementsByClassName('nav-tabs'),
  3. updateAddress = function(e) {
  4. if(history.pushState && e.target.dataset.link) {
  5. history.pushState(null, null, '#' + e.target.dataset.link);
  6. }
  7. };
  8. if (tabs.length > 0) {
  9. tabs = tabs[0].querySelectorAll('li');
  10. for (var i = 0; i < tabs.length; i++) {
  11. tabs[i].addEventListener('click', updateAddress);
  12. var linkTag = tabs[i].querySelector('a');
  13. if (location.hash !== '') {
  14. var currentHash = location.hash.substr(1);
  15. if (currentHash === linkTag.dataset.link) {
  16. linkTag.click();
  17. }
  18. }
  19. }
  20. }
  21. });