简体   繁体   English

如何使图像对象与Javascript链接

[英]How to make an image object a link with Javascript

HTML equivalent: 等同于HTML:

<a href='#'> <img src='...'....../></a>

Looking for something along the lines of: 寻找类似的东西:

var im = document.createElement("img");
im.ADDLINK;

Relevant code: 相关代码:

var close = document.createElement("img");
    close.src="close.png";
    close.width = 50;
    close.height = 50;
    close.border = 1;
    close.className="closeButton";
    close.style.cssFloat="right";

    var link = document.createElement("a");
    link.href = "#";

Make close image a link: 将近距离图片链接:

    link.appendChild(close);

    close.style.right=0+"px";
            //Div img is a div created above this code snippet:
    divimg.appendChild(close);

Create the link: 创建链接:

var link = document.createElement(a);
link.href = "http://...com";

Add the image: 添加图片:

var im = document.createElement("img");
link.appendChild(im);

And append it to the DOM where needed 并在需要时将其附加到DOM

You can try this 你可以试试这个

<script>
   function imgRedirect(destination){

     location.href = destination;

   }
</script>
<img src="..." onclick="imgRedirect('http://google.com')"/>

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

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