0

我一直在这个网站上搜索解决方案,但我看不到它。我对来自同一个 DIV 类 ID 的 slideToggle 有问题。

<script>
$(document).ready(function(){
  $("div.yeah").click(function(){
   $(this).next('ul').slideToggle();
  });
});

我的代码不起作用

<div class="yeah"><a href="#">Menu 1</a><br />
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
</div>


<div class="yeah"><a href="#">Menu 2</a><br />
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
</div>


<div class="yeah"><a href="#">Menu 3</a><br />
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
</div>

如何在不更改 HTML 的情况下分别切换其中的每一项,只允许更改 jquery。

4

3 回答 3

1

ul节点在节点内部div,而不是在它旁边:

$("div.yeah").click(function(){
    $(this).find('ul').slideToggle();
});
于 2013-08-07T08:17:20.383 回答
0

next() expects to search for siblings, so the same level: http://api.jquery.com/next/

Instead you can do:

$(this).find('ul').slideToggle();

Because find will look inside of the div.

Also, you should close your <ul>

于 2013-08-07T08:18:28.747 回答
0

First up, your DOM code is wrong(no close tag for ul)

Second I assume you need to access the ul inside the div, child('ul') instead of next('ul') since ul is a child element and they are not on the same level.

于 2013-08-07T08:19:37.607 回答