Get img tag id when click on map area of this image

你离开我真会死。 提交于 2019-12-24 16:53:04

问题


I have some images-figures with map-area like this

and i need get image id when click inside area zone
can i made it without custom area functional realisation with js?

UPD sandbox

UPD2 found second, other problem. first area zone (red), which covered by second image not clickable and z-index for area not working.

FINAL UPD
I wrote small custom function for mapping some objects.
Maybe it will help someone:
jsFiddle


回答1:


I hope this is what you are looking for

JS Fiddle Example : http://jsfiddle.net/g5Dy3/44/

HTML

<img id="dotted" src="image1.jpg" usemap="#ballmap" alt="sample" />
<img id="solid" src="image2" usemap="#ballmap2" alt="sample" />

<map name="ballmap">
    <area shape="circle" coords="210,120,90" href="#" alt="dotted ball" title="dotted ball" onclick="clickedMe(this);">
</map>
<map name="ballmap2">
    <area shape="circle" coords="126,90,70" href="#" alt="solid ball" title="solid ball" onclick="clickedMe(this);">
</map> 

JS

function clickedMe(item) {
    var mapName;
    mapName = $(item).parent().attr('name');
    $('img').each(function(){
        if($(this).attr('usemap') == '#'+mapName){
            alert($(this).attr('id'));            
        }       
    });
}



回答2:


Like so assuming you can use jQuery:

<img id="planetsimg" src="planets.gif" width="145" height="126" alt="Planets" usemap="#planetmap">

<map name="planetmap">
  <area shape="rect" coords="0,0,82,126" href="sun.htm" onclick="clicked(this)" alt="Sun" image-id="planetsimg">
  <area shape="circle" coords="90,58,3" href="mercur.htm" onclick="clicked(this)" alt="Mercury" image-id="planetsimg">
  <area shape="circle" coords="124,58,8" href="venus.htm" onclick="clicked(this)" alt="Venus" image-id="planetsimg">
</map>

<script>
function clicked(map) {
    var image = $('#' + $(map).attr('image-id'));
    alert('img ' + image + ' clicked map ' + $(map).attr('href'));

    return false; // Prevents href from opening normally. Return true if you want the link to open
</script>



回答3:


just add the clickhandlers to the areas (images) of your map, not to the map itself

if you are using jQuery, you can do it like that:

$('map').find('area').each(function() {
  $(this).on('click',... 
});


来源:https://stackoverflow.com/questions/13571861/get-img-tag-id-when-click-on-map-area-of-this-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!