多个jQuery切换链接 [英] Multiple jQuery toggle links

查看:100
本文介绍了多个jQuery切换链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法在页面上创建了多个切换实例,但是切换链接本身必须是唯一的,因为当前切换一个链接会切换其余所有链接.

I've managed to create multiple toggle instances on my page, however the toggle links themselves need to be unique, as at the moment toggling one link toggles all the rest.

到目前为止,这是我的代码:

Here is my code so far:

jQuery:

$(document).ready(function() {
  $('.toggle').click(function() { 
   var id = $(this).attr('name');
   $('#module' + id).slideToggle('fast');
   $('.toggle').toggle();
   return false; 
  });
});

HTML:

<a class="toggle" name="1" href="#">- Hide</a> 
<a class="toggle hidden" name="1" href="#">+ Show</a>

<div id="module1">Toggled Content</div>

如您所见,问题是"$('.toggle').toggle();"因为它不是每个切换唯一的.因此,切换一个链接关闭,关闭所有其他链接,并在打开时保持不变.有什么想法吗?

As you can see, the issue is "$('.toggle').toggle();" as it's not unique to each toggle. So toggling one link closed, closes all the others, and the same when opening. Any ideas?

推荐答案

切换两个链接:

$('[name=' + id + ']').toggle();

这篇关于多个jQuery切换链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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