Jquery如何测试当前悬停的div是否为eq(x)
我正在尝试创建一个 jQuery 悬停函数,该函数将根据 div 的 EQ(索引位置编号)数字突出显示页面的不同部分。
我想做的是说“当你将鼠标悬停在 #photoContent div 上时,检查它的 EQ 编号是什么。如果它是第 X 个 div,则突出显示侧栏中的第 Y 个 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');
});
上面的代码显然没有实现这一点,但是概念/功能就是我想要的。感谢您的帮助!
I am trying to create a jQuery hover function that will highlight a different section of the page depending on what number EQ (index position number) the div is.
What I want to do is say "when you hover over the #photoContent div, check what it's EQ number is. If it's the Xth div, then highlight the Yth p in the sidebar"
$('#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');
});
The above code obviously does not accomplish this, but the concept/functionality is what I'm going for. Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,hoverIntent 不是内置的 jQuery 事件。您的意思是
hover(fnOver, fnOut)
吗?其次,您可以重写
if
语句来显式测试匹配:OR
另外,这只是我的风格,但为什么不使用正则引号
"
而不是单引号呢?First, hoverIntent is not a built-in jQuery event. Do you mean
hover(fnOver, fnOut)
?Second, you could rewrite the
if
statements to explicitly test for a match:OR
Also, this is just my style, but why not use regular quotes
"
instead of single quotes?我相信我使用 index() 属性找到了我正在寻找的内容:
这有帮助:
在 Jquery 中,如何找出被单击元素的“eq”?
I Believe I found what I was looking for, using the index() property:
This helped:
In Jquery how do you find out the 'eq' of an element of what is being clicked?
我不知道为什么你的代码不起作用,除了杰夫指出的
hoverIntent
用户之外,但考虑到它们都在同一个文档中,这有点令人费解,你不能去吗with:我也不知道为什么最后一点是一个函数,因为你可能希望它只在悬停时触发。
我不确定上面的方法是否有效,因为我对 filter() 方法非常不熟悉,但它看起来是正确的。它会让你更亲近吗?
I'm not sure why your code doesn't work, other than the user of
hoverIntent
as Jeff pointed out, but it's a tad convoluted given that it's all in the same document, couldn't you go with:I'm not sure why that last bit was a function either, since you probably want it to only fire when it's hovered.
I'm not sure the above will work, as I'm very unfamiliar with the filter() method, but it looks about right. does it get you closer?