简体   繁体   中英

Javascript toggle function: change text and alter class style

I have a really simple function that should:

  • change the wording (from expand to collapse)
  • change the style (visibility) for a class

It sorta works but really not

function toggle_expansion(id) {
var e = document.getElementById(id);
var es =getElementsByClass('expansion');
if (e.innerHTML ='expand all')  {        
   e.innerHTML ='collapse all';
   for(i=0; i<es.length; i++) 
   {es[i].style.display = 'none';}
  }  
else {       
   e.innerHTML ='expand all';
   for(i=0; i<es.length; i++) 
   {es[i].style.display = 'block';}
  }
}

and the html:

<a href='#nogo' onclick="toggle_expansion('expand');"><div id='expand'>expand all</div></a>

The text change works the first time it's clicked but only the one time and the class style change doesn't work (although it has worked in other snippets).

There are no errors thrown - it just doesn't work...Anyone have any ideas?

if (e.innerHTML ='expand all')  {

应该

if (e.innerHTML == 'expand all')  {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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