插入可点击的图片 [英] Inserting a clickable image
问题描述
我想插入一个可点击的图片,也就是说,如果图片的任何部分被点击,它将会像一个按钮元素一样,或者更好的是运行一个Javascript脚本。
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?
推荐答案
有很多方法可以做到这一点。
There are lots of ways to do this.
<img id="Img" src="img.jpg" />
添加一个onclick属性:
Adding an onclick attribute:
<img id="Img" src="img.jpg" onclick="myFunction()" />
在脚本中添加onclick事件侦听器:
Adding onclick event listener from script:
document.getElementById( "Img" ).onclick = function() {
// img clicked
};
将图片设为按钮背景
<input type="button" style="background: url( img.jpg )" />
这篇关于插入可点击的图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!