在 jQuery 上:很多项目仅显示单击的该锚点的 div

发布于 2024-08-09 03:24:00 字数 982 浏览 2 评论 0原文

首先:对不起我的英语

我有一个像这样的网页

<div class="item"><div class="details">
<ul>
<li><a href="#">Show div 1</a></li>
<li><a href="#">Show div 2</a></li>
<li><a href="#">Show div 3</a></li>
</ul>
<div>div 1</div>
<div>div 2</div>
<div>div 3</div>           </div></div>

重要提示:我有几个名为“item”的div。

我有这个 jquery 脚本

$(document).ready(function() {
  $('ul > li > a').each(function(index) {
  $(this).click(function() {
  $('.details > div:eq(' + index + ')').toggle()
  .siblings('.details > div').hide();
  });
  });
});

,但是如果我单击“a”,它会显示所有项目的相对“div”。 我想如果我单击第一个元素的第一个“a”,它会显示第一个唯一项目的第一个“div”,而不是所有项目。

这是一个测试页面链接文本 (:O,它仅适用于第一个 div 上的链接!)

我希望已经清楚了..

谢谢大家

first of all: sorry for my english

i have a webpage like this

<div class="item"><div class="details">
<ul>
<li><a href="#">Show div 1</a></li>
<li><a href="#">Show div 2</a></li>
<li><a href="#">Show div 3</a></li>
</ul>
<div>div 1</div>
<div>div 2</div>
<div>div 3</div>           </div></div>

IMPORTANT: i have several divs called 'item'.

i have this jquery script

$(document).ready(function() {
  $('ul > li > a').each(function(index) {
  $(this).click(function() {
  $('.details > div:eq(' + index + ')').toggle()
  .siblings('.details > div').hide();
  });
  });
});

BUT if i click on 'a' it shows me the relative 'div' for ALL items.
Id like if i click on first 'a' of the first element it shows me the first 'div' of the first ONLY item, not all items.

here's a test page link text
(:O it works only with links on first div!)

I hope to have been clear..

Thanks all

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷路的信 2024-08-16 03:24:00

如果您能够向 div 和链接添加类属性。

<div class="item"><div class="details">
<ul>
<li><a href="#" id="div1">Show div 1</a></li>
<li><a href="#" id="div2">Show div 2</a></li>
<li><a href="#" id="div3">Show div 3</a></li>
</ul>
<div class="div1">div 1</div>
<div class="div2">div 2</div>
<div class="div3">div 3</div>           </div></div>

然后,您的 jQuery 可以简单如下:

$(document).ready(function() {
  $('ul > li > a').click(function(){
          $('#details > div').hide(); // hide all of the divs
          $('div.' + this.id).show(); // then show the one with the same class as the link.id that was clicked
       });
    });

If you're able to add a class attribute to the divs and links.

<div class="item"><div class="details">
<ul>
<li><a href="#" id="div1">Show div 1</a></li>
<li><a href="#" id="div2">Show div 2</a></li>
<li><a href="#" id="div3">Show div 3</a></li>
</ul>
<div class="div1">div 1</div>
<div class="div2">div 2</div>
<div class="div3">div 3</div>           </div></div>

Then, your jQuery can be as simple as:

$(document).ready(function() {
  $('ul > li > a').click(function(){
          $('#details > div').hide(); // hide all of the divs
          $('div.' + this.id).show(); // then show the one with the same class as the link.id that was clicked
       });
    });
谜泪 2024-08-16 03:24:00

编辑,哎呀! INDEX 搞砸了, div:eq(' + index + ') 总是在寻找第一个项目 div 的第一个、第二个和第三个元素,试试这个:

$(document).ready(function() {
  $('ul > li > a').each(function(index) {
    $(this).click(function() {
        $(this).closest('.item').siblings().find('.details > div').hide();
        $('.item .details div:eq(' + index + ')').toggle();
    });
  });
});

并尝试合并一个更简单的解决方案,就像@Dave Beer 的那样。

EDIT, ouch! the INDEX was messing it up, div:eq(' + index + ') was always looking for the first, second and third elements of the first item div, try this:

$(document).ready(function() {
  $('ul > li > a').each(function(index) {
    $(this).click(function() {
        $(this).closest('.item').siblings().find('.details > div').hide();
        $('.item .details div:eq(' + index + ')').toggle();
    });
  });
});

And try to incorporate a simpler solution, like @Dave Beer's.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文