Hoping someone can help as I do not know much about JS
I have 3 divs
<div id="content1">This is content 1 </div>
<div id="content2">This is content 2 </div>
<div id="content2">This is content 2 </div>
I require some JS that randomly loads one of those divs on page load and hide the other two
Any help would be much appreciated
Thanks
You can select all div
elements when the page loads, then pick a random one to keep and hide the rest.
var elems = $("div"); if (elems.length) { var keep = Math.floor(Math.random() * elems.length); for (var i = 0; i < elems.length; ++i) { if (i !== keep) { $(elems[i]).hide(); } } }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="content1">This is content 1 </div> <div id="content2">This is content 2 </div> <div id="content3">This is content 3 </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.