Home>

how to make a picture a link in css, if in html pictures pop up randomly, that is, for each picture there is a separate link

< script language= "Javascript" > < ! -BEGIN var images= 3; var whichImage= Math.round (Math.random () * (images-1)) + 1;

document.write ('< div id= "img_prehome '+ whichImage +'" > & nbsp; < /div > '); //END -> < /script >

  • Answer # 1
    var i= [
      ['vk.com', 'http: //cs424830.vk.me/v424830492/67ee/AQeO7bBxCco.jpg'],
      ['facebook.com', 'http: //www.formulaworldshop.com/images/externe/facebook.jpg'],
      ['gmail.com', 'http: //www.italia-news.it/media/contenuti/gmail.jpg']
    ];
    i= i [(~~ (Math.random () * i.length))];
    document.write ('&lt;
    a href= '+ i [0] +' &gt;
    &lt;
    img src= '+ i [1] +' &gt;
    &lt;
    /a &gt;
    ');
    

    Replace links and image addresses with your own. And there will be what is usually a banner rotator. Example of work on jsbin...

    on the site I wanted to make sure that when the page was refreshed in a separate div there were different pictures, displayed randomly. And so that by clicking on them you can go to sites, that is, attach a specific site to a specific picture. I managed to change the images, but there are no links yet ... (Sorry if I wrote something wrong, since I am making a website for the first time and at the same time I am learning html, css on my own ...)

    dragon00772021-02-23 04:30:47

    edited the answer to reflect your comment. see an example of work and, if you are satisfied, replace your code with mine.

    fogrew2021-02-23 04:30:47
  • Answer # 2

    Like this

    < script >

    (function () {
            var images= 3;
            var whichImage= Math.round (Math.random () * (images-1)) + 1;
            var href= ['# href1', '# href2', '# href3'];
            document.write ('&lt;
    a href= "'+ href [whichImage-1] +'" &gt;
    &lt;
    div id= "img_prehome '+ whichImage +'" &gt;
    &
    nbsp; &lt;
    /div &gt;
    &lt;
    /a &gt;
    ');
        } ())
    &lt;
    /script &gt;