Can I have an onclick event on a imagemap area element?(我可以在 imagemap 区域元素上设置 onclick 事件吗?)
问题描述
我想在区域元素上放置一个 onclick 事件.这是我的设置:
I would like to put an onclick event on an area element. Here is my setup:
<img id="image" src="wheel.png" width="2795" height="2795" usemap="#Map" >
<map name="Map">
<area class="blue" onclick="myFunction()" shape="poly" coords="2318,480,1510,1284" href="#">
</map>
我尝试了 2 种不同的方式来设置 onclick 事件.首先我尝试了这个:
I have tried 2 different ways to have an onclick event. Firstly i tried this:
$(".blue").click( function(event){
alert('test');
});
我也试过这个:
function myFunction() {
alert('test');
}
以上都不起作用.区域元素是否支持上述内容,还是仅支持具有 href?
Neither of the above work. Do area elements support the above, or do they only support having a href?
推荐答案
注意:
属性 href 是强制性的,没有它,area-tag 什么都做不了!
Attribute href is obligatory, without it the area-tag does nothing!
要添加点击事件,您需要屏蔽默认 href.
To add a click event, you'll need to block default href.
<小时>
你的代码应该如下开始:
Your code should start as follows:
$(".blue").on("click", function(e){
e.preventDefault();
/*
your code here
*/
});
现场示例.
这篇关于我可以在 imagemap 区域元素上设置 onclick 事件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!