简体   繁体   English

HTML上的Jquery选择器。 第一和兄弟姐妹

[英]Jquery Selectors on HTML. First and Siblings

I have been tasked with writing 10 different selectors for a piece of HTML. 我的任务是为一个HTML编写10个不同的选择器。 It seems I am stuck on the last two. 看来我被困在最后两个。 I have no idea how to get the correct things to be highlighted. 我不知道如何正确显示要突出的内容。 Any help would be greatly appreciated. 任何帮助将不胜感激。 Here is the code that the query is executing on. 这是执行查询的代码。

 <ul id="links">
     <li><a href="link1">1</a></li>
     <li><a href="link2">2</a></li>
     <li><a href="link3">3</a></li>
     <li><a href="link4">4</a></li>
     <li>
         <p>Some other links</p>
         <ul>
             <li><a href="link5">5</a></li>
             <li><a href="link6">6</a></li>
         </ul>
     </li> 
 </ul>

I am trying to write two jquery selectors that do the following two things: 我正在尝试编写两个jquery选择器,它们执行以下两件事:

  1. The first link in each Unordered List. 每个无序列表中的第一个链接。 (would select link 1 and link 4) (将选择链接1和链接4)
  2. All sibling links after link 2 链接2之后的所有同级链接

Any help would be greatly appreciated. 任何帮助将不胜感激。 I know I am to use the :first keyword for 1...but I cant seem to get it to select both that are needed. 我知道我将:first关键字用于1 ...但是我似乎无法让它选择所需的两个。 For 2, I have no idea how to even begin. 对于2,我什至不知道如何开始。

I think you want to select the first li in your lists, and then all other li's if I read your post correctly. 我认为您想选择列表中的第一个li,如果我正确阅读了您的帖子,则选择所有其他li。 Try this: 尝试这个:

$(document).ready(function() {
   $('li').css('background-color','transparent');    
   $('li:first-child').css('background-color','yellow');
});

First one is easy: 第一个很简单:

firstLi = $( "li:first a", "ul" ); // - selects 1 and 5, first element of each list

For the second item, do you mean all siblings of the links? 对于第二项,您是指链接的所有兄弟姐妹吗? Since each link is contained in its own li, none of the links have siblings per se. 由于每个链接都包含在自己的li中,因此这些链接本身都没有同级。 You can test it: 您可以对其进行测试:

$( "a" ).siblings(); // empty array - no matches, because the links have no direct siblings

If you are looking for siblings of li elements, use the .siblings() selector. 如果您正在寻找li元素的兄弟姐妹,请使用.siblings()选择器。 Building on the previous example, select all the siblings of the first two lis, then bold them: 在上一个示例的基础上,选择前两个lis的所有同级,然后将其加粗:

$( "li:first", "ul" ).siblings().css("font-weight","bold");

jsfiddle: http://jsfiddle.net/mqzjehrd/ jsfiddle: http : //jsfiddle.net/mqzjehrd/

You can do this with pure CSS. 您可以使用纯CSS做到这一点。

First child is easy in CSS: :first-child . 在CSS :first-child一个孩子很容易。 all siblings after the second list item is a bit harder, but can be done. 第二个列表项之后的所有兄弟姐妹都比较难,但是可以做到。 Use the general sibling selector ~ . 使用通用的同级选择器~

See it in action: http://jsfiddle.net/w4xg3r9c/ 实际观看: http : //jsfiddle.net/w4xg3r9c/

ul > li:first-child {color: red;}

li:nth-child(2) ~ li {color: green;}

You can exclude the item that has a sub menu by adding a class to those items in HTML and then exclude that item from the selection in CSS, like so . 您可以排除通过添加一个类来HTML的项目有子菜单项,然后排除该项目从选择的CSS,像这样

li:nth-child(2) ~ li:not(.has-submenu) {color: green;}

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

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