代码之家  ›  专栏  ›  技术社区  ›  HandiworkNYC.com

Jquery如何测试当前悬停的div是否等于eq(x)

  •  0
  • HandiworkNYC.com  · 技术社区  · 16 年前

    我想说的是“当你把鼠标悬停在photoContent div上时,检查它的EQ值是多少。如果是第x个div,则突出显示侧边栏中的Yth p“

        $('#photoContent div').hover(function () {  
            if( $(this).filter(':lt(5)') ) {
                $('#photoSidebar').find('p:eq(0)').addClass('currentPhoto');
            }
    
            if( $(this).filter(':gt(5)') ) {
                $('#photoSidebar').find('p:eq(1)').addClass('currentPhoto');
            }
        }, function () {
            $('#photoSidebar').find('p').removeClass('currentPhoto');
        });
    

    上面的代码显然不能实现这一点,但是我要介绍的是概念/功能。谢谢你的帮助!

    3 回复  |  直到 16 年前
        1
  •  3
  •   Jeff Meatball Yang    16 年前

    首先,hoverIntent不是内置的jQuery事件。你的意思是 hover(fnOver, fnOut)

    第二,你可以重写 if 要显式测试匹配的语句:

    if($(this).is(":lt(5)")) { 
        $('#photoSidebar p').eq(0).addClass('currentPhoto');
    }
    
    if($(this).is(":gt(5)")) {
        $('#photoSidebar p').eq(1).addClass('currentPhoto');
    } 
    

    或者

    var idx = $(this).index();
    if(idx > 5) { 
        $('#photoSidebar p').eq(0).addClass('currentPhoto');
    }
    
    if(idx < 5) {
        $('#photoSidebar p').eq(1).addClass('currentPhoto');
    } 
    

    "

        2
  •  2
  •   Community Mohan Dere    9 年前

    我相信我找到了我要找的东西,使用index()属性:

        var divIndex = $(this).index();
            if (divIndex<=4) {
                  //...
            }
    

    这有助于: In Jquery how do you find out the 'eq' of an element of what is being clicked?

        3
  •  0
  •   Anthony    16 年前

    hoverIntent 正如杰夫所指出的,但考虑到这些都在同一份文件中,这有点复杂,你不能同意:

    $('#photoContent div').hover(function () {    
        if( $(this).filter(':lt(5)') ) {
            $('#photoSidebar p:eq(0)').addClass('currentPhoto');
        }
    
        if( $(this).filter(':gt(5)') ) {
            $('#photoSidebar p:eq(1)').addClass('currentPhoto');
        }
    
        $('#photoSidebar p').removeClass('currentPhoto');
    });