简体   繁体   English

我该如何过滤 <li> 在一个 <ul> 有特定的ID?

[英]How do I filter <li> under a<ul> having a particular id?

        <ul class="apple" id="A">
             <li>
             <li>
              .....
              ......
              ......

        </ul>

         <ul class="apple" id="B">
             <li>
             <li>
              .....
              ......
              ......

        </ul>

In the above code I get the ul having id B using jQuery selector. 在上面的代码中,我使用jQuery选择器获取具有ID B的ul。

$(selector).find('ul.apple #B') This does not seem to work. $(selector).find('ul.apple #B')这似乎不起作用。

I want to display all the li under the ul having class=apple & id ="B" . 我想在所有具有类= apple&id =“ B”的ul下显示li。 The above code is in FTL ( freemarker) & I am trying to trigger the id written in freemarker from the js code. 上面的代码在FTL(freemarker)中,我正在尝试从js代码触发以freemarker编写的ID。

Thanks :) 谢谢 :)

Id's should be unique in a document. ID在文档中应该是唯一的。 You never need to include any other selector but the id itself $('#B') . 除了ID本身$('#B')您无需再包含任何其他选择器。 No need for .find or anything - there's only one on the page anyway. 不需要.find或任何东西-无论如何页面上只有一个。

If you want a reference to the li tags under #B , here are two ways: 如果要引用#B下的li标签,可以采用以下两种方法:

//get a reference to the list
var $B = $('#B');
//get children (which are lis)
var $lis = $B.children();

or just get the lis directly: 或者直接获取lis:

var $lis = $('#B li');

Normall you only need to specify the id, as it's unique in the page, so a selector like this would work: 正常情况下,您只需指定ID,因为它在页面中是唯一的,因此这样的选择器将起作用:

#B li

If you still need to specify the element and class, for example if you are using the same CSS for several pages, you would specify them without spaces between: 如果仍然需要指定元素和类,例如,如果您在多个页面上使用相同的CSS,则可以指定它们之间没有空格:

ul.apple#b li

In the HTML 4.01 specification , it is specified that element IDs must be unique . HTML 4.01规范中 ,指定元素ID必须唯一

That said, you can just query by the id via: 也就是说,您可以通过以下方式按ID查询:

$('#A');

通过ID和类名称选择UI:$('B .apple')

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

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