简体   繁体   English

选择除兄弟姐妹和自我之外的所有元素

[英]Selecting all elements EXCEPT siblings and self

Two unordered lists. 两个无序列表。 When hovering over one list item I need to darken the list items in the other list, but not the hovered list item's own siblings. 将鼠标悬停在一个列表项上时,我需要使另一列表中的列表项变暗,而不是使悬停的列表项自己的同级变暗。

With the code below I'm able to darken all other list items but the one being hovered, but I can't seem to get the siblings into the equation. 使用下面的代码,我可以使所有其他列表项变暗,但将其悬停,但我似乎无法将兄弟姐妹纳入等式。

HTML: HTML:

<ul>
                <li class="tn">
                        <img class="tn-img" src="http://lorempixel.com/360/200" />
                </li>
                <li class="tn">
                        <img class="tn-img" src="http://lorempixel.com/360/200" />
                </li>
                <li class="tn">
                        <img class="tn-img" src="http://lorempixel.com/360/200" />
                </li>
</ul>
<ul>
                <li class="tn">
                        <img class="tn-img" src="http://lorempixel.com/360/200" />
                </li>
                <li class="tn">
                        <img class="tn-img" src="http://lorempixel.com/360/200" />
                </li>
                <li class="tn">
                        <img class="tn-img" src="http://lorempixel.com/360/200" />
                </li>
</ul>

JQUERY: JQUERY:

$('.tn').hover(
function() {
    $(".tn:not(:hover)").not(this).css({"-webkit-filter":"brightness(15%)"});
},
function() {
    $(".tn").css({"-webkit-filter":"brightness(100%)"});
}
);

And for anyone who suggests I should just do the hover over the entire unordered list, it's not possible because of certain restrictions of the design :/ 对于建议我应该将鼠标悬停在整个无序列表上的任何人,由于设计的某些限制,这是不可能的:/

html: HTML:

<ul class="tnparent">

javascript: JavaScript的:

$('.tnparent:not(:hover) .tn').css({"-webkit-filter":"brightness(15%)"})

Use .closest('ul') to find this list, and then operate just on its siblings. 使用.closest('ul')查找此列表,然后对其同级对象进行操作。

 $('.tn').hover( function() { var thislist = $(this).closest('ul'); var otherlists = thislist.siblings('ul'); otherlists.find('.tn').css({ "-webkit-filter": "brightness(15%)" }); }, function() { $(".tn").css({ "-webkit-filter": "brightness(100%)" }); } ); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul> <li class="tn"> <img class="tn-img" src="http://lorempixel.com/360/200" /> </li> <li class="tn"> <img class="tn-img" src="http://lorempixel.com/360/200" /> </li> <li class="tn"> <img class="tn-img" src="http://lorempixel.com/360/200" /> </li> </ul> <ul> <li class="tn"> <img class="tn-img" src="http://lorempixel.com/360/200" /> </li> <li class="tn"> <img class="tn-img" src="http://lorempixel.com/360/200" /> </li> <li class="tn"> <img class="tn-img" src="http://lorempixel.com/360/200" /> </li> </ul> 

You want the opposite ul so lets traverse to the parent of current one, and jump over to the other 您想要相反的ul,因此让我们遍历当前一个的父对象,然后跳到另一个

$('.tn').hover(function(){
   var $currentList = $(this).parent();
   var $otherListItems = $currentList.siblings('ul').find('.tn');
   $otherListItems.doSomething();

},function(){
  /* similar */

})

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

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