简体   繁体   English

检测用jQuery单击了哪个选项卡

[英]Detect which tab was clicked with jQuery

I have some tabs: 我有一些标签:

<ul id="tabs">
    <li><a href="#tab-allData">All data</a></li>
    <li><a href="#tab-someOtherData">Some other data</a></li>
    <li><a href="#tab-xyData">xyData</a></li>
</ul>

I want to recognize which tab was clicked and remove the tab- prefix from the href . 我想识别单击了哪个选项卡,并从href删除了tab-前缀。

I have tried this js function: 我试过这个js函数:

$('#tabs').click(function (event) {        
    activeTab = $(this).attr('href').split('-')[1];        
    FurtherProcessing(activeTab);        
});

but I get the following error: 但是我收到以下错误:

TypeError: $(...).attr(...) is undefined activeTab = $(this).attr('href').split('-')[1]; TypeError:$(...)。attr(...)未定义activeTab = $(this).attr('href')。split(' - ')[1];

<ul id="tabs">
<li><a href="#tab-allData">All data</a></li>
<li><a href="#tab-someOtherData">Some other data</a></li>
<li><a href="#tab-xyData">xyData</a></li>
</ul>

$('#tabs').on("click", "li", function (event) {         
  var activeTab = $(this).find('a').attr('href').split('-')[1];
  FurtherProcessing(activeTab);        
});

Demo: http://jsfiddle.net/6dRH6/2/ 演示: http //jsfiddle.net/6dRH6/2/

Use this: attribute-starts-with-selector 使用此: attribute-starts-with-selector

$('[href^=tabs]').click(function (event) {        
    activeTab = $(this).attr('href').split('-')[1];        
    FurtherProcessing(activeTab);        
});

And remove # from html of href. 并从href的html中删除#

you can write li click event and get its anchor tag attribute: 你可以写li click事件并获得它的锚标记属性:

$('li').click(function (event) {        
    activeTab = $(this).find('a').attr('href').split('-')[1];        
    FurtherProcessing(activeTab);        
});

FIDDLE DEMO FIDDLE DEMO

Use a class its better for your future coding... 使用一个更好的类来为您将来的编码...

<li><a href="#tab-allData" class="tabs">All data</a></li>
<li><a href="#tab-someOtherData" class="tabs">Some other data</a></li>
<li><a href="#tab-xyData" class="tabs">xyData</a></li>


$('.tabs').click(function (event) {        
    var activeTab = $(this).attr('href').split('-')[1];
        alert(activeTab)
});

Working fiddle link... http://jsfiddle.net/JQnE3/ 工作小提琴链接... http://jsfiddle.net/JQnE3/

I'm using jQuery's on method to make use of event delegation. 我正在使用jQuery的方法来使用事件委托。 This only binds one event listener to the ul element instead of one for each tab. 这只将一个事件监听器绑定到ul元素,而不是每个选项卡绑定一个。 You will notice the "a" selector in the on method. 你会注意到on方法中的“a”选择器。 This makes use of event bubbling to know that it was the a tag inside the ul that was clicked. 这使用事件冒泡来知道它是被点击的ul内的标签。

This is the fastest and most efficient way: 这是最快速,最有效的方式:

http://jsperf.com/complicated-jquery-selectors http://jsperf.com/complicated-jquery-selectors

HTML HTML

<ul id="tabs">
    <li><a href="#tab-allData">All data</a></li>
    <li><a href="#tab-someOtherData">Some other data</a></li>
    <li><a href="#tab-xyData">xyData</a></li>
</ul>

JS JS

$("#tabs").on("click", "a", function (event) {        
    var activeTab = $(this).attr('href').split('-')[1];
    FurtherProcessing(activeTab);        
});

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

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