javascript - 当我们单击特定选项卡时,如何隐藏 Accordion 菜单项中打开的所有选项卡?

标签 javascript jquery html css

我有一个要求,当我点击任何选项卡时,它像 Accordion 菜单选项卡一样工作,但这里的一个问题是当我点击任何其他选项卡时,打开的选项卡应该关闭,只有当前选项卡相关的内容应该显示,因为我提到的 html 只是示例,实际上 id 和 div 是动态生成的。我也插入图片以便更好地理解。 enter image description here

<script	src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"/>
<script>
	$(document).ready(function(){
		

	// Get all the links.
	var link = $("#vy_accordion a");
	

	// On clicking of the links do something.
	link.on('click', function(e) {

	    e.preventDefault();

	    var a = $(this).attr("href");

	    $(a).slideToggle('fast');
	    
	    $(".control-group").css({ 'display' : 'block', 'margin-bottom' : '0' });

	    
	});
	
	});
	
	</script>
#vy_accordion {
	margin-top: 10px;
	border: thin solid #cecece;
	border-top: none;
	border-bottom: none;
}

#vy_accordion div {
	background: white;
	/*height: inherit;
	line-height: inherit;*/
	display: none;
	border-bottom: thin solid #cecece;
	padding-left: 15px;
	min-height: 70px;
}

a.divlink {
	display: block;
	/* width: 483px; */
	background: #f4f4f4;
	background-image: -webkit-linear-gradient(white, #ededed);
	background-image: -moz-linear-gradient(white, #ededed);
	background-image: -o-linear-gradient(white, #ededed);
	background-image: -ms-linear-gradient(white, #ededed);
	background-image: linear-gradient(white, #ededed);
	color: #959696;
	padding-left: 15px;
	height: 40px;
	line-height: 40px;
	text-decoration: none;
	border-bottom: thin solid #cecece;
	border-top: thin solid #cecece;
	font-family: Arial;
	font-size: 13px;
	font-weight: bold;
	text-shadow: 0px 1px 1px white;
}
<a class="divlink" href="#Menu-hover-color">Menu-hover-color</a>
<div id="Menu-hover-color" style="display: none;"> 
  <div class="control-group"> 
    <label class="control-label" for="_156_Menu-hover-color"> Menu-hover-color </label> <input class="field" id="_156_Menu-hover-color" name="" type="text" value="#B3E5FC">  
  </div> 
</div>
<a class="divlink" href="#Menu-hover-color">Menu-item-color</a>
<div id="Menu-item-color" style="display: none;"> 
  <div class="control-group"> 
    <label class="control-label" for="_156_Menu-item-color"> Menu-hover-color </label> <input class="field" id="_156_Menu-item-color" name="" type="text" value="#B3E5FC">  
  </div> 
</div>

最佳答案

将类似的类添加到您的选项卡(例如 linkTab)并基于此类选择器调用 hide() 在显示单击/选择的选项卡之前,如下所示:

HTML:

<a class="divlink" href="#Menu-hover-color">Menu-hover-color</a>
    <div class="linkTab" id="Menu-hover-color" style="display: none;">
...

JS:

var link = $("#vy_accordion a");

// On clicking of the links do something.
link.on('click', function(e) {
    e.preventDefault();

    var a = $(this).attr("href");

    // this line will hide all open tab based on class selector
    $('.linkTab').hide();

    $(a).slideToggle('fast');

    $(".control-group").css({ 'display' : 'block', 'margin-bottom' : '0' });

});

DEMO

关于javascript - 当我们单击特定选项卡时,如何隐藏 Accordion 菜单项中打开的所有选项卡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33433459/

相关文章:

Javascript indexOf 对象未找到

javascript - Jquery - 如何从链接 href 获取页码并用作类

php - AJAX DropDown 未填充

javascript - 使用 HTML/JS 将输入保存到文本文件?

javascript - 如果对象是通过引用复制的,为什么 JavaScript 垃圾回收会这样工作?

javascript - Ajax查询json源码并输出结果

javascript - setInterval() - 但只运行一次

javascript - 背景的流体/连续图像滚动

javascript - Ubuntu 8.04 Hardy 和 node.js Upstart 脚本

jquery - knockoutjs IE 8 : Couldn't find any memo with ID ed2d1f3121ff7e. 也许它已经被取消内存