简体   繁体   English

Javascript切换可见性的多个div

[英]Javascript toggle visibility multiple divs

http://blog.movalog.com/a/javascript-toggle-visibility/ http://blog.movalog.com/a/javascript-toggle-visibility/

this is a page with some code and a script im using in my site for an image gallery, however when trying to toggle the visibility of multiple div's it only works on the first. 这是一个页面,其中包含一些代码和一个脚本im在我的网站中用于图片库,但是,当尝试切换多个div的可见性时,它仅在第一个上起作用。 can someone please fix it to work with multiple div's, i dont know js :) 有人可以修复它以与多个div一起工作,我不知道js :)

here is the javascript 这是JavaScript

<script type="text/javascript">
<!--
    function toggle_visibility(id) {
       var e = document.getElementById(id);
       if(e.style.display == 'block')
          e.style.display = 'none';
       else
          e.style.display = 'block';
    }
//-->
</script>

and here is the html code for the links 这是链接的html代码

<tr><td><a href="#" onclick="toggle_visibility('nyc');">New York</a></td>
<td><a href="#" onclick="toggle_visibility('photoshop');">Photoshop Work</td>
<td><a href="#" onclick="toggle_visibility('photography');">Photography</td></tr>
<tr><td><a href="#" onclick="toggle_visibility('art');">Art Projects</td></tr>

wait a sec, could this not be working because it is trying to acess the properties of multiple divs via the "id" property, would it work with the class property and if so would i just change the java script where it says "id" to "class" 等待一秒钟,这可能无法正常工作,因为它正在尝试通过“ id”属性访问多个div的属性,是否可以与class属性一起使用,如果可以的话,我将更改“ id”所在的Java脚本去“上课”

It seems that you were trying something like 看来您正在尝试类似

<div id="a"></div>
<div id="a"></div>

toggle_visibility('a');

The problem is that each id must be unique in the document, so document.getElementById returns a single element, not a collection of elements. 问题在于每个id在文档中必须是唯一的,因此document.getElementById返回单个元素,而不是元素集合。

Then, if you want to have more than one element with the same id, you should use classes instead. 然后,如果要具有多个具有相同ID的元素,则应改用类。

<div class="a"></div>
<div class="a"></div>


function toggle_visibility(cl){
   var els = document.getElementsByClassName(cl);
   for(var i=0; i<els.length; ++i){
      var s = els[i].style;
      s.display = s.display==='none' ? 'block' : 'none';
   };
}
toggle_visibility('a');

If you want to make it work with multiple classes, use 如果要使其与多个类一起使用,请使用

var toggle_visibility = (function(){
   function toggle(cl){
       var els = document.getElementsByClassName(cl);
       for(var i=0; i<els.length; ++i){
          var s = els[i].style;
          s.display = s.display==='none' ? 'block' : 'none';
       };
   }
   return function(cl){
      if(cl instanceof Array){
         for(var i=0; i<cl.length; ++i){
            toggle(cl[i]);
         }
      }else{
         toggle(cl);
      }
   };
})();
toggle_visibility('myclass');
toggle_visibility(['myclass1','myclass2','myclass3']);

You can use 您可以使用

function toggle_visibility(id) {
   function toggle(id){
      var el = document.getElementById(id);
      el.style.display = el.style.display==='none' ? 'block' : 'none';
   }
   if(id instanceof Array){
      for(var i=0; i<id.length; ++i){
         toggle(id[i]);
      }
   }else{
      toggle(id);
   }
}

And call it like 并称它为

toggle_visibility('myid');
toggle_visibility(['myid1','myid2','myid3']);

Another possible way is using arguments variable, but that could slow down your code 另一种可能的方法是使用arguments变量,但这可能会降低代码速度

function toggle_visibility() {
   function toggle(id){
      var el = document.getElementById(id);
      el.style.display = el.style.display==='none' ? 'block' : 'none';
   }
   for(var i=0; i<arguments.length; ++i){
      toggle(arguments[i]);
   }
}

And call it like 并称它为

toggle_visibility('myid');
toggle_visibility('myid1','myid2','myid3');

If you don't want to create the function toggle each time you call toggle_visibility (thanks @David Thomas), you can use 如果您不想在每次调用toggle_visibility时都创建函数toggle (感谢@David Thomas),则可以使用

var toggle_visibility = (function(){
   function toggle(id){
      var el = document.getElementById(id);
      el.style.display = el.style.display==='none' ? 'block' : 'none';
   }
   return function(id){
      if(id instanceof Array){
         for(var i=0; i<id.length; ++i){
            toggle(id[i]);
         }
      }else{
         toggle(id);
      }
   };
})();
toggle_visibility('myid');
toggle_visibility(['myid1','myid2','myid3']);

Or 要么

var toggle_visibility = (function(){
   function toggle(id){
      var el = document.getElementById(id);
      el.style.display = el.style.display==='none' ? 'block' : 'none';
   }
   return function(){
      for(var i=0; i<arguments.length; ++i){
         toggle(arguments[i]);
      }
   };
})();
toggle_visibility('myid');
toggle_visibility('myid1','myid2','myid3');

You either need to cycle through a list of ids or use a class name as the argument to toggle_visibilty ---- which means you would have to edit the function. 您要么需要循环浏览ID列表,要么使用类名作为toggle_visibilty ----的参数,这意味着您必须编辑该函数。 It looks like right now you are only calling toggle_visibility on one element. 看起来现在您只在一个元素上调用toggle_visibility。

jQuery makes this kind of thing easier: jQuery使这种事情变得更容易:

  <code>
  //selects all elements with class="yourClassName"
  jQuery(".yourClassName").toggle();

  //select all divs
   jQuery("div").toogle();

  //select all divs inside a container with id="myId"
 jQuery("#myId > div").toggle();
 </code>

您的代码中有一个非常愚蠢的错误。在td标签中添加id属性id ='nyc',依此类推,它应该可以正常工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM