隐藏或显示特定的DIV

我有两个DIV。 一个有一个列表,另一个有DIV的集合。

 
Some content one
Some content two
Some content three

当我点击超链接’One’时,我想显示第一个DIV(一些内容一个)并隐藏所有其他DIV。 当我点击超链接’Two’时,我想要显示第二个DIV(一些内容两个)并隐藏所有其他内容。 如何使用jQuery中的数组实现此目的? 任何其他方法也没关系。

谢谢你的时间。

 $('li').click(function() { var which = $(this).index(); $('div').find('div').hide().eq(which).show(); }); 

工作小提琴

有没有搞错:

 $('a')​​.on('click', function(e) { e.preventDefault(); $('div>div').eq($(this).closest('li').index()).show().siblings().hide(); })​;​ 

小提琴

改变一下你的HTML:

 
  • One
  • Two
  • Three
Some content one
Some content two
Some content three

然后是jQuery:

 $('ul li span[data-showclass]').click(function(){ $('#swichDiv .show').hide().filter($(this).data('showclass')).show(); }); 

多行用于唯一行。

 // show hide in jquery $('.showdata').hide(); // hide all on start $('.showhide').click(function (e) { //e.preventDefault(); var SH = this.SH ^= 1; // "Simple toggler" $(this).text(SH ? 'Hide' : 'Show') var id = event.target.id; // get id //alert(event.target.id); $('#data' + id).toggle(); // show this one }); 

Show content1

Show content2

最简单的方法是给div和锚定对应的id和class

检查一下..它仍然可以优化

HTML

  

使用Javascript

 $('[id^="wrapper"]').on('click', function(e) { e.preventDefault(); $('.wrap > div').hide(); $('.'+ this.id).show(); });​ 

检查小提琴