显示Div并使用Href onclick隐藏其他分区。

时间:2022-12-02 17:51:04

I have a menu with some links. Like this:

我有一个带有一些链接的菜单。是这样的:

<ul id="nav"><li><a href="#" class="tartalom">Some Div 1</a></li><li><a href="#" class="tartalom2">Some Div 2</a></li></ul> 

Here are my divs.

这是我的div。

  <div class="tartalom1>Bla bla bla</div> 

  <div class="tartalom2>Yeeah yeah yeah</div> 

How to make it possible, if I click on the links to open that div with the same class and close the others if they are open? I tried this, but it wont help:

如何使它成为可能,如果我点击链接打开该div与相同的类并关闭其他的如果它们是开放的?我试过了,但没有用。

   $(document).ready( function(){
  $('.tartalom').click( function(){ // set of divs to be clickable
   $(this).siblings('div').hide(); // it's already showing, right?
});
});

1 个解决方案

#1


2  

You need to reach the enclosing div first to access siblings divs. You can use closest() function to find the nearest ancestor matching the search criteria.

您需要首先到达封闭分区,以访问兄弟姐妹。您可以使用最近的()函数找到与搜索条件匹配的最近的祖先。

Live Demo

现场演示

$(document).ready( function(){
  $('.tartalom').click( function(){ // set of divs to be clickable
      $(this).closest('div').siblings('div').hide(); // it's already showing, right?
  });
});

You also missed the closing quotes of tartalom1 and tartalo2 class in divs. 

你也错过了tartalom1和tartalo2课程的结尾部分。

#1


2  

You need to reach the enclosing div first to access siblings divs. You can use closest() function to find the nearest ancestor matching the search criteria.

您需要首先到达封闭分区,以访问兄弟姐妹。您可以使用最近的()函数找到与搜索条件匹配的最近的祖先。

Live Demo

现场演示

$(document).ready( function(){
  $('.tartalom').click( function(){ // set of divs to be clickable
      $(this).closest('div').siblings('div').hide(); // it's already showing, right?
  });
});

You also missed the closing quotes of tartalom1 and tartalo2 class in divs. 

你也错过了tartalom1和tartalo2课程的结尾部分。