English 中文(简体)
jQuery代码使特定类的所有div在单击链接时切换
原标题:jQuery code causes all divs of a particular class to toggle when link is clicked
  • 时间:2011-02-08 23:48:58
  •  标签:
  • jquery

我有一些jquery代码,可以在单击链接时显示和隐藏div。

$(document).ready(function() {
  $(".tracklist").hide();
  $( .link ).bind( click , function(e) {
    $( .tracklist ).toggle( slide , {easing:  easeOutQuint , direction:  down }, 1000);
    return false;
  });
});

当我按下任何链接时,这段代码都会切换所有名为tracklist的div。不好。

我需要找到一种方法来确保每个链接只影响一个tracklistdiv

Edit

我的标记-下面的代码在每页上显示多次:

<article class="podcast">
  <ul class="buttons">
    <li>
      <a class="link" href=>"#">Tracklist</a>
    </li>
    // other links removed 
  </ul>

  <div class="tracklist">
    Content that I want to toggle
  </div>
</article>
最佳回答

固定:

$(document).ready(function() {
 $(".tracklist").hide();
 $( .link ).bind( click , function(e) {
  $(this).closest( .podcast ).children( .tracklist ).toggle( slide , {easing:  easeOutQuint , direction:  down }, 1000);
       return false;
    });
});

现在它成功了:)感谢@Loktar,他让我成功了90%

问题回答

尝试

$(document).ready(function() {
    $(".tracklist").hide();
    $( .link ).bind( click , function(e) {
       $(this).children( .tracklist ).toggle( slide , {easing:  easeOutQuint , direction:  down }, 1000);
    return false;
 });
});  

您可以向链接添加一个rel属性,该属性与轨迹列表id相匹配。

例如:

<div class="tracklist" id="track-1"></div>
<div class="tracklist" id="track-2"></div>
<div class="tracklist" id="track-3"></div>

<a href="#" class="link" rel="1">Show 1</a>
<a href="#" class="link" rel="2">Show 2</a>
<a href="#" class="link" rel="3">Show 3</a>

<script>
$("a.link").click(function(){
  var el = $(this);
  $("div.tracklist#track-" + el.attr("rel")).show();
});
</script>




相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签