简体   繁体   English

如何在一个页面上单击链接并同时更改另一个页面上特定 class 的位置?

[英]How to click on a link on one page and change the location of a specific class on another page at the same time?

I designed two pages on the website.I want the second tab - btn, instead of the first tab - btn, to have id = "default" and accept the activate class when the link on page 1 is clicked and goes to page 2. As you can see in the html code, there is normally id = "default" and the activate class in tab1.But when the link on page 1 is clicked and page 2 appears, instead of tab1, the second tab has id = "default" and an activate class. Meanwhile, tab1 lacks id = "default" and the class is activated.我在网站上设计了两个页面。我希望第二个选项卡 - btn,而不是第一个选项卡 - btn,具有 id = "default" 并在单击第 1 页上的链接并转到第 2 页时接受激活 class。 html代码中可以看到,tab1中正常id=”default”,activate class。但是当点击第1页的链接出现第2页时,不是tab1,而是第2个tab的id=”default " 和激活 class。同时,tab1 缺少 id = "default" 并且 class 被激活。 Can I manage this with JavaScript?我可以用 JavaScript 管理吗? Thank you in advance for your cooperation, friends先谢谢各位朋友的配合

 //codes of javascript for page: 2 function tabs(e,name){ var tab_btn = document.getElementsByClassName('tab--btn'); var tab_content = document.getElementsByClassName('tab--content'); var supporItemsCard = document.querySelector('.support--items__card'); var i; for( i = 0; i<tab_btn.length; i++){ tab_btn[i].classList.remove('active'); } for(i = 0; i<tab_content.length; i++){ tab_content[i].style.display = 'none'; } document.getElementById(name).style.display = 'block'; e.currentTarget.classList.add('active'); } document.getElementById('default').click();
 page: 1 <a href=""> link page 1 </a> page: 2 <ul class="support--items__cards"> <li class="support--items__card tab--btn" id="default" onclick="tabs(event,'tab1')"> <h4 class="support--items__card__title">TAB 1</h4> </li> <li class="support--items__card tab--btn" onclick="tabs(event,'tab2')"> <h4 class="support--items__card__title">TAB 2</h4> </li> <li class="support--items__card tab--btn" onclick="tabs(event,'tab3')"> <h4 class="support--items__card__title">TAB 3</h4> </li> <ul> <div class="content"> <div id="tab1" class="tab--content"> content tab 1 </div> <div id="tab2" class="tab--content"> content tab 2 </div> <div id="tab3" class="tab--content"> content tab 3 </div> </div>

You can check the referrer page on page 2 and execute your already existing function tabs to go to second tab.您可以检查第 2 页上的引荐页面,并执行您现有的 function 标签到 go 到第二个标签。 At that time remove id from first tab and add that attribute to the second li.那时从第一个选项卡中删除 id 并将该属性添加到第二个 li。 I removed event from tab function and used tabid only.我从选项卡 function 中删除了事件并仅使用了 tabid。

<ul class="support--items__cards">
  <li class="support--items__card tab--btn" id="default"     onclick="tabs('tab1')">
        <h4 class="support--items__card__title">TAB 1</h4>
    </li>

  <li class="support--items__card tab--btn" onclick="tabs('tab2')">
    <h4 class="support--items__card__title">TAB 2</h4>
  </li>

  <li class="support--items__card tab--btn" onclick="tabs('tab3')">
    <h4 class="support--items__card__title">TAB 3</h4>
  </li>
</ul>

<div class="content">
  <div id="tab1" class="tab--content">
      content tab 1
  </div>

  <div id="tab2" class="tab--content">
      content tab 2
  </div>

  <div id="tab3" class="tab--content">
      content tab 3
  </div>
</div>

<script>       
    var referrer =  document.referrer;
    //alert(referrer);
    
    if(referrer === "page1.html"){
        tabs('tab2');            
        var supporItemsCard = document.getElementsByClassName('support--items__card');
        supporItemsCard[0].removeAttribute("id");
        supporItemsCard[1].setAttribute("id", "default");            
    }

//codes of javascript for page: 2
function tabs(name){
    var tab_btn = document.getElementsByClassName('tab--btn');
    var tab_content = document.getElementsByClassName('tab--content');
    var supporItemsCard = document.querySelector('.support--items__card');
    var i;

    for( i = 0 ; i<tab_btn.length ; i++){
        tab_btn[i].classList.remove('active');
    }

    for(i = 0 ; i<tab_content.length ; i++){
        tab_content[i].style.display = 'none';
    }

    document.getElementById(name).style.display = 'block';
    document.getElementById(name).classList.add('active');

}
</script>

    

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过单击另一页的链接更改一页中的 state? - How to change state in one page from a link click from another page? phantomjs click适用于一个链接,但不适用于同一页面中的另一个链接 - phantomjs click works on one link but not another in the same page 如何点击链接上的链接和呼叫功能同时进行链接? - How to click on a link and call function on the page that link go to as the same time? 单击页面的链接时,更改另一页面中同一菜单的链接的颜色 - change the color of the link of the same menu in another page when you click on a link of a page 如何将链接指向另一个页面上加载到iframe中的页面,然后单击一下即可将其滚动到位置? - How do I direct a link to a page loaded into an iframe on another page and scroll it to location with a single click? 单击链接到新页面,在新页面上更改类别 - Click link to a new page, change class on new page 如何单击一个页面上的链接并在链接页面上选择特定的过滤器选项? - How click a link on one page and have a specific filter option chosen on linked page? 如何进行链接单击以保留在同一页面上 - how to make a link click to stay on same page 单击链接如何在同一页面中显示表格? - How to display a table in a same page click on a link? 单击html5中的按钮/链接时如何将一个页面导航到另一个页面 - how to navigate one page to another page when click on button/link in html5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM