在 jQuery 上:很多项目仅显示单击的该锚点的 div
首先:对不起我的英语
我有一个像这样的网页
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您能够向 div 和链接添加类属性。
然后,您的 jQuery 可以简单如下:
If you're able to add a class attribute to the divs and links.
Then, your jQuery can be as simple as:
编辑,哎呀! INDEX 搞砸了,
div:eq(' + index + ')
总是在寻找第一个项目 div 的第一个、第二个和第三个元素,试试这个:并尝试合并一个更简单的解决方案,就像@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:And try to incorporate a simpler solution, like @Dave Beer's.