javascript jQuery 一次滑动多个 div 同一个类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5615890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 17:46:25  来源:igfitidea点击:

jQuery slidetoggle multiple divs one at a time with same class

javascriptjqueryhtmlslidetoggle

提问by Ed R

I'm trying to use slideToggle()on multiple div hover's, showing another div in its respected container div. Example:

我试图slideToggle()在多个 div 悬停上使用,在其受人尊敬的容器 div 中显示另一个 div。例子:

<div class="container">123
   <div class="content">ABC</div>
</div>
<div class="container">123
   <div class="content">ABC</div>
</div>

It runs on a dynamic page so the number of containers could range from 1-25. I am trying to slideToggle the "content"class of each div on the container hover. I use this jQuery

它在动态页面上运行,因此容器的数量可以在 1-25 之间。我试图"content"在容器悬停上滑动切换每个 div的类。我使用这个 jQuery

function slide() {
   $(".content").slideToggle("fast");
   return false;
}

$(".container").hover(slide, slide);

It will only work on the first container/content div however. How can I make it slidetoggle each created div without 25 different jQuery functions? Any help is appreciated, thank you.

但是,它仅适用于第一个容器/内容 div。我怎样才能让它在没有 25 个不同 jQuery 函数的情况下滑动每个创建的 div?任何帮助表示赞赏,谢谢。

回答by Hussein

$(".container").hover(function(){
$(this).find('.content').slideToggle();
});

Make sure you hide .contentby default.

确保.content默认隐藏。

.content{
 display:none;
} 

check working example

检查工作示例

回答by Khez

You need to relate the container with the right content.

您需要将容器与正确的内容相关联。

function slide() {
   $(this).find(".content").slideToggle("fast");
   return false;
}

$(".container").hover(slide, slide);

Here's an example on jsfiddle.

这是一个关于 jsfiddle例子

P.S. why are you returning false in slide ?

PS为什么你在幻灯片中返回false?

回答by Ben

Heres another possiblity: http://jsfiddle.net/UfjMe/5/

这是另一种可能性:http: //jsfiddle.net/UfjMe/5/

Works by looping through each item, hiding, then adding the hover command.

通过循环遍历每个项目,隐藏,然后添加悬停命令来工作。