简体   繁体   English

如何在链接中显示图像名称

[英]How to display the image name in the link

I have here a code where you can display a random image but I want to show the name of the image in the link of my website. 我这里有一个代码,您可以在其中显示随机图像,但我想在我的网站链接中显示图像的名称。 Example: mywebsite.com/random.html=funnycat.png (or something like that) Here is my code It would be nice if someone could help me with that problem. 示例:mywebsite.com/random.html=funnycat.png(或类似的东西)这是我的代码如果有人可以帮助我解决这个问题,那就太好了。 Also is it even posible with HTML or do I need php? HTML还是可能的,还是我需要php?

    <!DOCTYPE html>
    <html>
    <script>
    function display_random_image() 
    {
     var theImages = [{
        src: 
        "http://farm4.staticflickr.com/3691/11268502654_f28f05966c_m.jpg",
        width: "240",
        height: "160"
      }, {
        src: "http://farm1.staticflickr.com/33/45336904_1aef569b30_n.jpg",
        width: "320",
        height: "195"
        }, {
        src: "http://farm6.staticflickr.com/5211/5384592886_80a512e2c9.jpg",
        width: "500",
        height: "343"
       }];

        var preBuffer = [];
       for (var i = 0, j = theImages.length; i < j; i++) {
          preBuffer[i] = new Image();
        preBuffer[i].src = theImages[i].src;
        preBuffer[i].width = theImages[i].width;
        preBuffer[i].height = theImages[i].height;
        }

        // create random image number
        function getRandomInt(min,max) 
         {
      //  return Math.floor(Math.random() * (max - min + 1)) + min;

imn = Math.floor(Math.random() * (max - min + 1)) + min;
    return preBuffer[imn];
    }  

// 0 is first image,   preBuffer.length - 1) is  last image

var newImage = getRandomInt(0, preBuffer.length - 1);

// remove the previous images
var images = document.getElementsByTagName('img');
var l = images.length;
for (var p = 0; p < l; p++) {
    images[0].parentNode.removeChild(images[0]);
}
// display the image  
document.body.appendChild(newImage);
}
    </script>
<head>
<meta charset=utf-8 />
<title>Display a random image.</title>
<style type="text/css">
body {margin-top: 30px;}
</style> 
</head>
<body>
<div>
<button id="jsstyle" 
onclick="display_random_image();">Show Image</button> 
</div>
</body>
</html>

请在您的示例上尝试。

  preBuffer[i].title = theImages[i].src;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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