作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
首先:抱歉我的英语不好
我有一个这样的网页
<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”,而不是所有项目。
这是一个测试页link text(:哦,它只适用于第一个 div 上的链接!)
我希望已经清楚了..
谢谢大家
最佳答案
如果您能够将类属性添加到 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
});
});
关于jquery - 在 jQuery : with a lot of items show only div of this anchor clicked 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1657099/
我是一名优秀的程序员,十分优秀!