jQuery 获取具有活动类的类的 id?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20720043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
get the id of the class which has class active?
提问by PythonEnthusiast
I want to get the id of the id of the element which has class active. I've viewed this thread, but isnt able to get the o/p I want.
我想获取具有活动类的元素的 id 的 id。我已查看此线程,但无法获得我想要的 o/p。
<ul class="nav nav-pills nav-stacked navigation" id="configuration_sidebar_content">
<li>
<a>Location</a>
<ul class="nav nav-pills nav-stacked " id="location" style="padding-left:30px;">
<li class="active" id="region"><a>Region</a></li>
<li id="country"><a>Country</a></li>
</ul>
</li>
<li>
<a>Device</a>
<ul class="nav nav-pills nav-stacked" style="padding-left:30px;">
<li id="gateway_unit"><a>Gateway Unit</a></li>
<li id="meter_unit"><a>Meter Unit</a></li>
</ul>
</li>
</ul>
I triedthis.
我试过这个。
selected_item_id = $("#configuration_sidebar_content li.a.active").attr('id')
回答by Arun P Johny
it should be
它应该是
$("#configuration_sidebar_content li.active").attr('id')
there is no class a
assigned to the li
element, a
is a child element of the li
element
没有a
分配给li
元素的类,a
是元素的子li
元素
回答by Micha? Rybak
You code didn't work, because you wrote .a
, which means having class a
, which wasn't what you're looking for.
Try this:
您的代码不起作用,因为您编写了.a
,这意味着拥有 classa
,这不是您想要的。
尝试这个:
$("#configuration_sidebar_content").find(".active").attr('id')
Using find()
is more effectivethan selectors with spaces inside (e.g. li a
).
使用find()
是更有效的比空间内(如选择li a
)。
回答by Anto Subash
you can also try
你也可以试试
$("#configuration_sidebar_content li").find("a.active").attr('id')
回答by Muhammad Rashid
Try this , #configuration_sidebar_content li.a.active
change to $("#configuration_sidebar_content li.active").attr('id')
试试这个,#configuration_sidebar_content li.a.active
改成$("#configuration_sidebar_content li.active").attr('id')