检测使用jQuery单击了哪个选项卡 [英] Detect which tab was clicked with jQuery

查看:70
本文介绍了检测使用jQuery单击了哪个选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些标签:

<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>

我想识别单击了哪个选项卡,然后从href中删除tab-前缀.

I want to recognize which tab was clicked and remove the tab- prefix from the href.

我尝试了这个js函数:

I have tried this js function:

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

但出现以下错误:

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

TypeError: $(...).attr(...) is undefined 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);        
});

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

这篇关于检测使用jQuery单击了哪个选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆