jQuery:如何获取当前激活选项卡的id
我有一些标签如下 我需要获取当前激活选项卡的id,我尝试了很多方法,但仍然没有成功jQuery:如何获取当前激活选项卡的id,jquery,html,Jquery,Html,我有一些标签如下 我需要获取当前激活选项卡的id,我尝试了很多方法,但仍然没有成功 1. $('ul#apptabs li a').find('active').attr('id') 2. $('ul#apptabs li a.active').attr('id') 3. $('ul#apptabs nav-item').find('a.active').attr('id') 片段: console.log( $('ul#apptabs li a').find('active').attr(
1. $('ul#apptabs li a').find('active').attr('id')
2. $('ul#apptabs li a.active').attr('id')
3. $('ul#apptabs nav-item').find('a.active').attr('id')
片段:
console.log(
$('ul#apptabs li a').find('active').attr('id'),
$('ul#apptabs li a.active').attr('id'),
$('ul#apptabs nav item').find('a.active').attr('id'))
);代码>
我希望我能正确回答你的问题
您想要
的id
,对吗
然而,激活状态在
我希望我能正确回答你的问题
您想要
的id
,对吗
然而,激活状态在
一种可能的方法是返回父节点
console.log($('ul#apptabs li a.active').parent().attr('id'))
一种可能的方法是返回父节点
console.log($('ul#apptabs li a.active').parent().attr('id'))
我会建议.parent()
,因为我们已经知道当前元素是一个
。在这个特定情况下,您是对的。可能需要注意的是,当OP添加一个额外的元素时,parent()
将不起作用,但是closest()
将起作用。我会建议.parent()
,因为我们已经知道当前元素是一个
。在这个特定情况下,您是对的。可能需要注意的是,当OP添加一个额外的元素时,parent()
将不起作用,但是closest()
将起作用。这不是一个重复的问题,这个答案不适合我的需要,我已经尝试过了。不是重复的问题,这个答案不适合我的需要,我已经试过了。