for循环中的jquery点击函数仅在最后一个循环中工作 [英] jquery click function in for loop only working in last loop

查看:315
本文介绍了for循环中的jquery点击函数仅在最后一个循环中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能做的事显然错了,但我只是看不到它。我试图使用一个for循环来定义多个点击事件,并遇到意想不到的结果。其中一些是工作(隐藏和显示在函数的开始,但两个部分卷起针对循环中的第二个项目。有人可以看看这个,告诉我我做错了什么谢谢你非常感谢您的帮助!

I'm probably doing something obviously wrong, but I just can't see it. I'm trying to use a for loop to define multiple click events and am experiencing an unexpected result. Some of this is working (the hide and show at the beginning of the function, but both sections wind up targeting the second item in the loop. Could somebody take a look at this and tell me what I'm doing wrong? Thank you so much for your help!

这里是链接:
http://grana.us/test/expand2.html

推荐答案

这是错误的...

首先...隐藏所有详细信息并显示所有工人简单使用:

First... to hide all details and show all toglers simply use:

$('.details').hide();
$('.toggler').show();

然后为所有汇总定义点击函数:

And then define click function to all sumaries:

$('.summary').click(function(){
   if($('.toggler',this).html() == ' -'){
      $('.toggler',this).html(' +');
      $('.details',$(this).parent()).hide();
   }else{
      $('.toggler',this).html(' -');
      $('.details',$(this).parent()).show();
   }
});

将所有内容放入...

Put everything in...

$(function(){
   ...
});

应该可以。

这篇关于for循环中的jquery点击函数仅在最后一个循环中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆