Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/440.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 单击其他链接时自动重置_Javascript_Html_Css - Fatal编程技术网

Javascript 单击其他链接时自动重置

Javascript 单击其他链接时自动重置,javascript,html,css,Javascript,Html,Css,下面的代码是当您单击某个div时显示的。隐藏项将显示并将css更改为显示块。我有多个菜单或选项,我想做的是当你点击某个分区,然后点击另一个分区。第一个自动关闭,这是div的指示符或id Html JS 将类例如“autohide”添加到所有要自动隐藏的div。然后: $('.autohide').click(function(){ $('.autohide').hide(); $(this).show(); }) 只需稍微调整一下代码,只需使用一个变量

下面的代码是当您单击某个div时显示的。隐藏项将显示并将css更改为显示块。我有多个菜单或选项,我想做的是当你点击某个分区,然后点击另一个分区。第一个自动关闭,这是div的指示符或id

Html

JS


将类例如“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';

}