简体   繁体   English

如何在jQuery中选择:#menu ul li:hover> ul?

[英]How to select in jQuery: #menu ul li:hover > ul?

How to select in jQuery an equivalent of #menu ul li:hover > ul ? 如何在jQuery中选择等同于#menu ul li:hover > ul

I would like to change the color of a box when hover is only on li TARGET 当悬停仅在li TARGET上时,我想改变盒子的颜色

For more details, see: http://jsfiddle.net/wGbwX/18/ 有关更多详细信息,请参阅: http//jsfiddle.net/wGbwX/18/

You could hover the parent of nested uls: 你可以悬停嵌套的uls的父级:

$('#menu ul > li >ul').parent().hover

EDITED to reflect pebbl's recomendation of using selecting nested li's 编辑以反映pebbl使用选择嵌套li的推荐

Updated JS fiddle: http://jsfiddle.net/wGbwX/23/ 更新了JS小提琴: http//jsfiddle.net/wGbwX/23/

Since your TARGET is in an a tag you can use the following: 因为你的TARGET是在a标签,你可以使用以下命令:

$('#menu > ul > li > a')

The > is to select the immediate child of each parent. >是选择每个父母的直接子女。 Then finally bind the hover event on the a tag instead of the li . 然后最后将aover事件绑定到a标签而不是li

The reason for using all those > in the selectors, is so that the selector doesn't select the a tag that are inside the TARGET li . 在选择器中使用所有那些>的原因是选择器不选择TARGET li a标签。

Edit: 编辑:

Since the HOME link should not trigger the hover event, you can use one of the following: 由于HOME链接不应触发悬停事件,因此您可以使用以下方法之一:
Both will select the same items, but the logic is different. 两者都会选择相同的项目,但逻辑是不同的。

$('#menu>ul>li:gt(0) > a')

jsfiddle . jsfiddle
This will make use of the :gt selector. 这将使用:gt选择器。

$('#menu>ul>li:not(:first) > a')

jsfiddle . jsfiddle
while this is a combination of the :first and the :not . 而这是:第一个第二个的结合:不是 jsfiddle. 的jsfiddle。

Edit 2: 编辑2:
Ok sorry, now I understand the requirements of the question. 对不起,现在我理解了问题的要求。

Joel's solution works, but if you wanted to do the whole thing in the selector, you can do the following: Joel的解决方案有效,但如果您想在选择器中执行整个操作,则可以执行以下操作:

$('#menu > ul > li:has(ul) > a')

jsfiddle . jsfiddle
This will make use of the the :has selector. 这将使用:has选择器。 I tend to prefer this solution, for the single fact that, if I needed to return to this code later on, or someone else wanted to review the code, the goal of the selector can be easily understood by reading it. 我倾向于选择这个解决方案,因为如果我稍后需要返回此代码,或者其他人想要查看代码,那么通过阅读它可以很容易地理解选择器的目标。

I am selecting an anchor inside a list item which contains an unordered list , etc... 我在list item选择一个包含unordered list等的anchor ...

Try this one: 试试这个:

$('#menu ul:first > li > a')

Updated fiddle . 更新了小提琴

i think this works: 我认为这有效:

but only for jquery 1.4 and higher 但仅适用于jquery 1.4及更高版本

$("#menu ul li ul li").hover(function(){}); $(“#menu ul li ul li”)。hover(function(){});

try this demo 试试这个演示

$('#menu ul li a').hover(

    function(e) {
      change = $(this).parent().find(".sub-nav").length;
      if(change)
      {
        $('.toto').css({'background-color':'#cccccc'});
      }

    },
    function() { $('.toto').css({'background-color':'#f8f8f8'}); }
 );

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

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