Javascript 单击其他链接时自动重置
下面的代码是当您单击某个div时显示的。隐藏项将显示并将css更改为显示块。我有多个菜单或选项,我想做的是当你点击某个分区,然后点击另一个分区。第一个自动关闭,这是div的指示符或id Html JSJavascript 单击其他链接时自动重置,javascript,html,css,Javascript,Html,Css,下面的代码是当您单击某个div时显示的。隐藏项将显示并将css更改为显示块。我有多个菜单或选项,我想做的是当你点击某个分区,然后点击另一个分区。第一个自动关闭,这是div的指示符或id Html JS 将类例如“autohide”添加到所有要自动隐藏的div。然后: $('.autohide').click(function(){ $('.autohide').hide(); $(this).show(); }) 只需稍微调整一下代码,只需使用一个变量
将类例如“autohide”添加到所有要自动隐藏的div。然后:
$('.autohide').click(function(){
$('.autohide').hide();
$(this).show();
})
只需稍微调整一下代码,只需使用一个变量来跟踪当前打开的部分
var current_open_section;
function showhide(id){
//hide open section first
if(current_open_section){
current_open_section.style.display = 'none';
}
//let the selected section be current open section
current_open_section = document.getElementById(id);
//show current open section
current_open_section.style.display = 'block';
}
<script>
function showhide(id) {
var e = document.getElementById(id);
e.style.display = (e.style.display == 'block') ? 'none' : 'block';
}
</script>
<script>
$('a#mylink').click(function() {
$(this).toggleClass('red blue');
});
</script>
$('.autohide').click(function(){
$('.autohide').hide();
$(this).show();
})
var current_open_section;
function showhide(id){
//hide open section first
if(current_open_section){
current_open_section.style.display = 'none';
}
//let the selected section be current open section
current_open_section = document.getElementById(id);
//show current open section
current_open_section.style.display = 'block';
}