can i have an onclick event on a imagemap area ele

2019-01-14 21:16发布

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>

I have tried 2 different ways to have an onclick event. Firstly i tried this:

$(".blue").click( function(event){
    alert('test');
});

I have also tried this:

function myFunction() {
    alert('test');
}

Neither of the above work. Do area elements support the above, or do they only support having a href?

Thanks in advance!

7条回答
甜甜的少女心
2楼-- · 2019-01-14 21:26

Pay attention:

  1. Attribute href is obligatory, without it the area-tag does nothing!

  2. 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
    */
});

Live example here.

查看更多
祖国的老花朵
3楼-- · 2019-01-14 21:39

Based on your comments, you just need this:

$("#image").click( function(){
 alert("clicked");
//your code here
});

Demo:

http://codepen.io/tuga/pen/waBQBZ

查看更多
家丑人穷心不美
4楼-- · 2019-01-14 21:44

Use a class for all elements you want to listen on, and optionally an attribute for behavior:

<map name="primary">
  <area shape="circle" coords="75,75,75" class="popable" data-text="left circle text">
  <area shape="circle" coords="275,75,75" class="popable" data-text="right circle text">
</map>
<img usemap="#primary" src="http://placehold.it/350x150" alt="350 x 150 pic">
<div class='popup hidden'></div>

Then add your event listeners to all elements in the class:

const popable = document.querySelectorAll('.popable');
const popup = document.querySelector('.popup');
let lastClicked;

popable.forEach(elem => elem.addEventListener('click', togglePopup));

function togglePopup(e) {
  popup.innerText = e.target.dataset.text;

  // If  clicking something else, first restore '.hidden' to popup so that toggle will remove it.
  if (lastClicked !== e.target) {
    popup.classList.add('hidden');
  }
  popup.classList.toggle('hidden');
  lastClicked = e.target;  // remember the target
}

Demo: https://codepen.io/weird_error/pen/xXPNOK

查看更多
何必那么认真
5楼-- · 2019-01-14 21:44

This is a simple one:

<area class="blue" onclick="alert('test');" shape="poly" coords="2318,480,1510,1284" href="#">
Or for any other code:

<area class="blue" onclick="//JavaScript Code//" shape="poly" coords="2318,480,1510,1284" href="#">

查看更多
男人必须洒脱
6楼-- · 2019-01-14 21:48

Try :

<img  src="wheel.png" width="2795" height="2795" alt="Weels" usemap="#map">

<map name="map">
 <area shape="poly" coords="2318,480,1510,1284" alt="otherThing" href="anotherFile">
</map>

You souldn't want to add onClick events on area, documentation :

The tag defines an area inside an image-map (an image-map is an image with clickable areas).

Edit : your coords are a bit weird since its supposed to the couples of each vertices (so right now, your polygon is a line)

查看更多
对你真心纯属浪费
7楼-- · 2019-01-14 21:51

Its the shape that's the problem. Your code has set shape equal to polygon but only has 4 points in the coordinates attribute. You need to set shape to rectangle instead.

Set shape="rect" like this:

<img id="image" src="wheel.png" width="2795" height="2795" usemap="#Map" > <map name="Map"> <area class="blue" onclick="myFunction()" shape="rect" coords="2318,480,1510,1284" href="#"> </map>

查看更多
登录 后发表回答