【问题标题】:When Tab is clicked replace window单击选项卡时替换窗口
【发布时间】:2021-10-16 13:46:29
【问题描述】:

我正在尝试为网站做一个语言切换器,我正在使用带有 2 个选项卡的选项卡元素 -

现在我正在尝试在单击其中一个选项卡时对 url 执行 window.location.replace。

所以标签 1 应该链接到 google.de,标签 2 应该链接到 google.com

我不太清楚如何定义每个标签

这就是我得到的当前代码

<script>
// when DOM ready
$(document).ready(function() {
  // if 'tab' item exists in localStorage
  if(localStorage.getItem('tab')){
    // click the respective tab button
    // e.g. if 'tab' item value == 0
    // click tab[0]
    $('.tab-button')[localStorage.getItem('tab')].click();
  }
});

// on tab click
$('.tab-button').click(function(){
  // get its index
  const index = $('.tab-button').index(this);
  // store the index in localStorage
  localStorage.setItem('tab',index);
});
</script>

【问题讨论】:

  • 请添加相关html

标签: javascript html webflow


【解决方案1】:
<script>
// when DOM ready
$(document).ready(function() {
  // if 'tab' item exists in localStorage
  if(localStorage.getItem('tab')){
    // click the respective tab button
    // e.g. if 'tab' item value == 0
    // click tab[0]
    $('.tab-button')[localStorage.getItem('tab')].click();
  }
});

// on tab click
$('.tab-button').click(function(){
  // get its index
  const index = $('.tab-button').index(this);
  // store the index in localStorage
  localStorage.setItem('tab',index);

  if (index == 1) {window.open('google.de', '_self');} 
  else if (index == 2) {window.open('google.com', '_self');}
  else {//open default window}

});
</script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-01
    • 2018-01-30
    • 1970-01-01
    • 1970-01-01
    • 2016-03-13
    • 1970-01-01
    • 2013-05-12
    相关资源
    最近更新 更多