jQuery 选择这个子项
<ul>
<li class="hoverMe"><div class="secret">Haha</div></li>
<li class="hoverMe"><div class="secret">Blabla</div></li>
<li class="hoverMe"><div class="secret">Tada</div></li>
</ul>
Secred div 隐藏在 css 中:
.secret {
display: none;
}
我想在悬停“hoverMe”后显示“hoverMe”的秘密子级(因此,当用户悬停第一个链接时,他会看到“哈哈”,第二个链接“Blabla”等)。
我尝试过这段代码不起作用,我一直在尝试用“child(ren)”等替换“next”,但没有什么想法吗?
jQuery().ready(function() {
jQuery('.hoverMe').hover(function(){
jQuery(this).next('.secret').Toggle();
});
});
奇怪的是 (this).children() 切换所有子项,但是。当我尝试使用 (this).children('.secret') 时,它没有做任何事情。
<ul>
<li class="hoverMe"><div class="secret">Haha</div></li>
<li class="hoverMe"><div class="secret">Blabla</div></li>
<li class="hoverMe"><div class="secret">Tada</div></li>
</ul>
Secred divs are hidden in css:
.secret {
display: none;
}
I want to display the secret child of "hoverMe" after hovering "hoverMe' (so when user hovers link number one he sees "Haha", nubmer two "Blabla" etc.).
I've tried this code and it doesn't work. I've been trying replacing "next" with "child(ren)" etc., but nothing. Any ideas?
jQuery().ready(function() {
jQuery('.hoverMe').hover(function(){
jQuery(this).next('.secret').Toggle();
});
});
The strange thing is (this).children() toggles all children, but when I try with (this).children('.secret') it doesn't do a thing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
hover() 可以绑定两个处理程序:
或者
或者,如 @Felix Kling 请注意,它只能绑定一个句柄:
hover() can bind two handlers:
or
or, as @Felix Kling kindly noticed, it can bind only one handle with:
当
.secret
未显示时,您的.hoverme
是空的(高度为 0),因此没有任何可悬停的内容。您需要使用
visibility:hidden
/visibility:visible
来代替示例:http://jsfiddle.net/bM7f2/
Your
.hoverme
s are empty (have a height of 0) when.secret
is not displayed, so there is nothing to hover over.You'll need to use
visibility: hidden
/visibility: visible
insteadExample: http://jsfiddle.net/bM7f2/
http://api.jquery.com/children/
哦,你试过了。没关系。
http://api.jquery.com/children/
Oh you tried that. never mind.
JS 区分大小写
JS is case sensitive
children
。toggle
需要以小写字母开头。所以应该是:
你的就绪处理程序也是错误的,必须是:
DEMO
children
.toggle
needs to start with a small letter.So it should be:
Your ready handler is also wrong, must be:
DEMO
如果这种情况发生在您身上并且似乎没有任何作用,请尝试将另一个 child() 添加到链中,或者至少检查您的标记以确保您的父元素不是真正的祖父元素。
答案是:
If it ever happens to you and nothing seem to work try adding another child() to the chain, or at least check your markup to make sure that your parent element is not really a GRANDparent.
The answer was: