jQuery选择一个类和这个类的“下一个”元素

时间:2013-11-14 17:51:45

标签: jquery select this next

是否可以选择两者 $('.foo')$(this).next('.subfoo') 对于相同的mouseenter?

我有一个代码,需要我以这种方式选择'.subfoo'的实例,并且我希望它在鼠标'.subfoo'上显示最近的'.foo'实例,而不是隐藏'.subfoo'直到鼠标离开。

我想我已经找到了部分代码,但我不知道如何在一个声明中选择它们。我无能为力。

感谢

2 个答案:

答案 0 :(得分:0)

您可以使用Multiple Selector语法

$('selector1, selector2, selectorN')

或JQuery add方法

var combinedSet = $(this).next('subfoo').add('.foo');
combinedSet.mouseenter(function() { 
    //do stuff in here
});

答案 1 :(得分:0)

我在这里开发的脚本是一个链接列表,每个链接都有一个弹出窗口,其中包含有关该链接的更多信息。 这个问题的目的是弄清楚如何通过将鼠标悬停在图标上来打开弹出窗口,并在鼠标离开图标后保持打开状态,然后进入弹出窗口。

部分困难在于同一页面上存在许多相同类型对象的实例,因此解决方案必须是通用的,并触发最接近悬停图标的弹出窗口。

HTML:

<li class="foo">
   <span class="icon"></span>
   <a href="">Title</a>
</li>
<li class="subfoo">
   Pop-out contents
</li>

JavaScript:

/*setup of variables and functions to be used*/

function clear(){ //set up a function to hide all pop-outs
$('.foo').each(function(){
$(this).next('.subfoo').hide()});}

var popoutHover = false; //initialize switch variable for following function:

$('.subfoo').mouseenter(function(){
popoutHover = true;}); //Set the variable to 'true' if the mouse is hovering over a pop-out

$('.subfoo').mouseleave(function(){
popoutHover = false; //Set the variable to 'false' and hide pop-outs if the mouse leaves
clear();
});

/*The main functionality*/

$('.icon').hoverIntent(function(){ //Hover over the icon for a link
        clear(); //Hide open pop-outs
        $(this)
            .closest('.foo') //Select the link next to the icon
            .siblings('.subfoo') //Select the pop-out for the link
            .slideDown(240)}, //Open the pop-out

function(){//If the mouse leaves the icon
        if (!popoutHover){ //And it is not hovering over a pop-out
        $(this)
            .closest('.foo') //Select the link
            .siblings('.subfoo') //Hide the pop-out
            .hide()}}
);

这是一个快速的小提琴,因为它可能表现得比我现在可以解释的更好: https://jsfiddle.net/kuzvgqkz/1/

相关问题