简体   繁体   English

多个选项卡列表:如何仅显示单击的选项卡列表的内容?

[英]Multiple tabs lists: How to show only the content of the clicked tab list?

I have several tabs that uses the event on('click') . 我有几个使用事件on('click')选项卡。 That problem is whenever I click a tab that triggers the event it also affects other tabs. 那个问题是,每当我单击触发事件的选项卡时,它也会影响其他选项卡。 How can I prevent it from firing or affecting other tabs. 如何防止其触发或影响其他标签。

Here is a sample of the tabs that I'm building on - 这是我正在构建的选项卡的示例-

 $(document).ready(function() { $('.tabs-list li:first').addClass('active'), $('.tab-content .show-content:first').addClass('active'); $('.tabs-list li').click(function(e) { event.preventDefault(); if(!$(this).hasClass('active')) { var tabIndex = $(this).index(); var nthChild = tabIndex + 1; $('.tabs-list li.active').removeClass('active'); $(this).addClass('active'); $('.tab-content .show-content').removeClass('active'); $('.tab-content .show-content:nth-child('+ nthChild +')').addClass('active'); } }) }) 
 .tabs-list li { display: inline-block; } .tab-content .show-content { display: none } .tab-content .show-content.active { display: block; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul class="tabs-list"> <li>Tab 1</li> <li>Tab 2</li> </ul> <div class="tab-content"> <div class="show-content"> Content 1 </div> <div class="show-content"> Content 2 </div> </div> <ul class="tabs-list"> <li>Tab 1</li> <li>Tab 2</li> </ul> <div class="tab-content"> <div class="show-content"> Content 1 </div> <div class="show-content"> Content 2 </div> </div> 

You can try accessing the event's target node and it's information to make sure you should execute. 您可以尝试访问事件的目标节点及其信息,以确保您应执行该事件。

ie

function modalBackgroundClick(e) {
    if (e.target.id == "modalContainer") {
        modalContainer.style.display = "none";
    }
}

This is checking that you actually clicked a modal background but it should be similar functionality 这是在检查您是否确实单击了模式背景,但功能应该相似

You have several .tabs-list and .tab-content in your page so when you use the selectors $('.tabs-list ...') and $('.tab-content ...') , you are not only selecting the clicked .tabs-list and its corresponding .tab-content but all the .tabs-list and .tab-content . 您的页面中有几个.tabs-list.tab-content ,因此当您使用选择器$('.tabs-list ...')$('.tab-content ...') ,您不会仅选择单击的.tabs-list及其对应的.tab-content而是选择所有.tabs-list.tab-content

You need to stick with $(this) (representing the clicked .tabs-list ) and select all the other elements from $(this) . 您需要坚持$(this) (代表单击的.tabs-list ),然后从$(this)选择所有其他元素。 jQuery provides several helpers to achieve it, like .parent() , .find() and .next() . jQuery提供了一些辅助工具来实现它,例如.parent() .find().next()

Here is a fixed version of your code, using those helpers: 这是使用这些帮助器的固定代码版本:

 $(document).ready(function() { $('.tabs-list li:first').addClass('active'), $('.tab-content .show-content:first').addClass('active'); $('.tabs-list li').click(function(e) { event.preventDefault(); if(!$(this).hasClass('active')) { var tabIndex = $(this).index(); var nthChild = tabIndex + 1; // select the right elements var $tabsList = $(this).parent(); var $tabContent = $tabsList.next('.tab-content'); $tabsList.find('li.active').removeClass('active'); $(this).addClass('active'); $tabContent.find('.show-content').removeClass('active'); $tabContent.find('.show-content:nth-child('+ nthChild +')').addClass('active'); } }) }) 
 .tabs-list li { display: inline-block; } .tab-content .show-content { display: none } .tab-content .show-content.active { display: block; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul class="tabs-list"> <li>Tab 1</li> <li>Tab 2</li> </ul> <div class="tab-content"> <div class="show-content"> Content 1 </div> <div class="show-content"> Content 2 </div> </div> <ul class="tabs-list"> <li>Tab 1</li> <li>Tab 2</li> </ul> <div class="tab-content"> <div class="show-content"> Content 1 </div> <div class="show-content"> Content 2 </div> </div> 

You need to link your tabs list and tab content. 您需要链接选项卡列表和选项卡内容。 Added a data-target attribute for the tabs-list that points to the tabs-content that you want to link the tabs with (or just use next() as GG suggested below) See adjusted JS accordingly 为tabs-list添加了一个数据目标属性,该属性指向您要与之链接标签的tabs-content(或仅使用next(),如下面的GG所示),请参见调整后的JS。

 $(document).ready(function() { $('.tabs-list li:first').addClass('active'), $('.tab-content .show-content:first').addClass('active'); $('.tabs-list li').click(function(e) { event.preventDefault(); var tabParent =($(e.currentTarget.parentElement)); var targetId = tabParent.data('target'); var tabContent = $(targetId); if(!$(this).hasClass('active')) { var tabIndex = $(this).index(); var nthChild = tabIndex + 1; tabParent.find('li.active').removeClass('active'); $(this).addClass('active'); tabContent.find('.show-content').removeClass('active'); tabContent.find('.show-content:nth-child('+ nthChild +')').addClass('active'); } }) }) 
 .tabs-list li { display: inline-block; } .tab-content .show-content { display: none } .tab-content .show-content.active { display: block; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul class="tabs-list" data-target="#contentContainer1"> <li>Tab 1</li> <li>Tab 2</li> </ul> <div class="tab-content" id="contentContainer1"> <div class="show-content"> Content 1 </div> <div class="show-content"> Content 2 </div> </div> <ul class="tabs-list" data-target="#contentContainer2"> <li>Tab 1</li> <li>Tab 2</li> </ul> <div class="tab-content" id="contentContainer2"> <div class="show-content"> Content 1 </div> <div class="show-content"> Content 2 </div> </div> 

.

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM