简体   繁体   English

jquery ul li选择器

[英]jquery ul li selector

I have this list 我有这个清单

<ul style="display:none">
  <li id="key1" title="Look, a tool tip!">item1 with key and tooltip
  <li id="key2" class="selected">item2: selected on init
  <li id="key3" class="folder">Folder with some children
    <ul>
      <li id="key3.1">Sub-item 3.1
      <li id="key3.2">Sub-item 3.2
    </ul>

  <li id="key4" class="expanded">Document with some children (expanded on init)
    <ul>
      <li id="key4.1">Sub-item 4.1
      <li id="key4.2">Sub-item 4.2
    </ul>
</ul>

Is there any way to select each < li > by its "id" with query? 有没有办法用查询选择每个<li>的“id”?

Ive tryed it like this but its now working 我曾经尝试过这样但现在正在努力

$("ul li:#key1").qtip({
         content: '<img src="icons/Icon.png" />'
    });

Thanx for the answers but none of the above was working.. Ive tryed this Thanx的答案,但上述都没有工作..我试过这个

$("ul li:first").qtip({
 content: '<img src="icons/Icon.png" />'
});

and im able to see the qtip.But only for the first on the list (key1). 并且我能够看到qtip.But仅用于列表中的第一个(key1)。 Trying

$("ul li#key1").qtip({
 content: '<img src="icons/Icon.png" />'
});

or 要么

$("#key1").qtip({
 content: '<img src="icons/Icon.png" />'
});

its not working for me.. =/ 它不适合我.. = /

Just use an id selector: 只需使用id选择器:

$("#key1").qtip(...);

id values must be unique on the page, and so leaving all the other stuff out lets jQuery use document.getElementByID , and the selector is nice and simple (eg, fast) to parse and apply. id必须在页面上是唯一的,因此保留所有其他东西让jQuery使用document.getElementByID ,并且选择器很好且简单(例如,快速)来解析和应用。 (Of course, if you want the selector to file if key1 isn't an li , then you need the full selector. But usually that's not what you want.) (当然,如果你想让选择器提交,如果key1不是li ,那么你需要完整的选择器。但通常这不是你想要的。)

Note that some of your id values will need escaping, because you have . 请注意,您的某些id值需要转义,因为您有. in them. 在他们中。 To escape a . 逃避一个. , you put a backslash in front of it in the selector. ,你在选择器前面放一个反斜杠。 Because the selector is in a JavaScript string, you have to escape the backslash to actually get the backslash into the selector, so you use two: 因为选择器是在JavaScript字符串中,所以必须转义反斜杠以实际将反斜杠放入选择器,因此您使用两个:

$("#key4\\.1").qtip(...);

Why the extra colon : ? 为什么额外结肠: You dont need it. 你不需要它。

$("ul li#key1").qtip({
     content: '<img src="icons/Icon.png" />'
});

Yes. 是。

$("ul li#key1").qtip(); //rest of code

Also, since it is an ID and, most likely, unique. 此外,因为它是一个ID,很可能是唯一的。 You can just simply use: 你可以简单地使用:

$("#key1")

If you are selecting by id, there is no need to specify the type, class or parent class. 如果按id选择,则无需指定类型,类或父类。 IDs should be unique. ID应该是唯一的。

As an example, you can use the following selector to select key1, key2 and key3 if you want to select all of them together: 例如,如果要一起选择所有选择器,可以使用以下选择器选择key1,key2和key3:

$("#key1, #key2, #key3").qtip({
         content: '<img src="icons/Icon.png" />'
    });

Or to select them one by one: 或者逐个选择它们:

$("#key1").qtip({
         content: '<img src="icons/Icon.png" />'
    });

$("#key2").qtip({
         content: '<img src="icons/Icon.png" />'
    });

Or you could do something like this to specify a list of keys and not repeat the selection and initialization of the qtip: 或者您可以执行类似这样的操作来指定键列表,而不是重复qtip的选择和初始化:

var keys = ['key1', 'key2', 'key3'];

$(keys).each(function(i, key) {
    $("#" + key).qtip({
         content: '<img src="icons/Icon.png" />'
    });

});

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

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