简体   繁体   中英

How to change css with javascript of multiple images - display: none to display: inline

So I would like the images to appear after I click the button.

HTML

<button class="knife-button" onclick="bayonetFunction()">Click to see more</button>

<div class="column1">
    <img src=""  class="bayonet" alt="">
</div>

<div class="column1">
    <img src=""  class="bayonet" alt="">
</div>

There s more images.

CSS

.bayonet {
      display: none;
 }

JS

function bayonetFunction(){
      document.getElementsByClassName('bayonet').style.display="inline";
}

This doesnt work.

 function bayonetFunction(){ [].forEach.call(document.querySelectorAll('.bayonet'), function(d) { d.style.display ="inline"; }); }
 .bayonet { display: none; }
 <button class="knife-button" onclick="bayonetFunction()">Click to see more</button> <div class="column1"> <img src="https://www.gravatar.com/avatar/3ad45b8fc3a096c8d840bcedb819bfb8?s=32&d=identicon&r=PG&f=1" class="bayonet" alt=""> </div> <div class="column1"> <img src="https://www.gravatar.com/avatar/3ad45b8fc3a096c8d840bcedb819bfb8?s=32&d=identicon&r=PG&f=1" class="bayonet" alt=""> </div>

You have to iterate over all elements. See this code example:

 function bayonetFunction(){ document.querySelectorAll('.bayonet').forEach(function(el) { el.style.display = 'block'; }); }
 .bayonet { display: none; width: 100px; height: 100px; background: teal; }
 <button class="knife-button" onclick="bayonetFunction()">Click to see more</button> <div class="column1"> <img src="" class="bayonet" alt=""> </div> <div class="column1"> <img src="" class="bayonet" alt=""> </div>

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