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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 01:16:53  来源:igfitidea点击:

get the id of the class which has class active?

jquery

提问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 aassigned to the lielement, ais a child element of the lielement

没有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.activechange to $("#configuration_sidebar_content li.active").attr('id')

试试这个,#configuration_sidebar_content li.a.active改成$("#configuration_sidebar_content li.active").attr('id')

http://jsfiddle.net/yWsRR/

http://jsfiddle.net/yWsRR/