Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
699 views
in Technique[技术] by (71.8m points)

html - Javascript toggle visibility multiple divs

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. can someone please fix it to work with multiple div's, i dont know js :)

here is the 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

<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"

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

Then, if you want to have more than one element with the same id, you should use classes instead.

<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']);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...