最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Inserting a clickable image - Stack Overflow

programmeradmin10浏览0评论

I want to insert a clickable image, that is, if any part of the image is clicked, it will either act like a button element, or more preferably, run a Javascript script.

I was thinking of inserting an image into a button element, but as far as I can tell, that would NOT just make the whole image into a button, but rather insert it into a button box.

How do I make a clickable image, that pulls up a script?

I want to insert a clickable image, that is, if any part of the image is clicked, it will either act like a button element, or more preferably, run a Javascript script.

I was thinking of inserting an image into a button element, but as far as I can tell, that would NOT just make the whole image into a button, but rather insert it into a button box.

How do I make a clickable image, that pulls up a script?

Share Improve this question asked Dec 17, 2011 at 2:12 Steven MatthewsSteven Matthews 11.3k48 gold badges137 silver badges252 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 14

There are lots of ways to do this.

<img id="Img" src="img.jpg" />

Adding an onclick attribute:

<img id="Img" src="img.jpg" onclick="myFunction()" />

Adding onclick event listener from script:

document.getElementById( "Img" ).onclick = function() {
    // img clicked
};

Put image as button background

<input type="button" style="background: url( img.jpg )" />

the best way would be to use an onClick attribute or add an event listener, but i personally like to use the anchor tag:

<a href = "#" ><img src = "#" ></a>
发布评论

评论列表(0)

  1. 暂无评论