Javascript + dynamic folders
So I have a Javascript function. It is passed a folder name, say "A". This folder contains 5 images which will be picked randomly.
It works well on the page load, as 1 random image appears. But I want it so that when the image is clicked, it is randomized again with an image from the same folder. I tried to go about this with the onmousedown, but it returns a broken image link to file:///C:/Users/StackOverflow/Documents/Website/[object%20HTMLDivElement]/2.jpg
<script type="text/javascript">
// <![CDATA[
function showImage(folder){
var randomNumber=Math.floor(Math.random()*5);
docu开发者_Go百科ment.write('<img src="'+folder+'/'+randomNumber+'.jpg" onmousedown="showImage('+folder+')">');
}
//]]>
</script>
I don't think you really want to be calling document.write()
every time the image is clicked. Perhaps you would have more success with a setup like:
<img id="randomImage0" src="" onmousedown="showImage(this, this.lastFolder);" />
<script type="text/javascript">
function showImage(imgElem, folder){
var randomNumber=Math.floor(Math.random()*5);
imgElem.src = folder + '/' + randomNumber + '.jpg";
imgElem.lastFolder = folder;
}
showImage(document.getElementById("randomImage0"), "A");
</script>
精彩评论