2015-04-07 40 views
1

当用户单击地图的一部分时,我尝试获取此部分的ID。Html地图对象 - 点击地图的一部分获取ID

<img src="images/mapa.png" alt="" width="588" height="711" usemap="#Map" /> 
<map name="Map" class="map"> 
    <area shape="circle" id="vrsac" coords="317,191,25" href="#"> 
    <area shape="circle" id="pancevo" coords="273,225,25" href="#"> 
    <area shape="rect" id="beograd" coords="195,247,251,276" href="#"> 
    <area id="lazarevac" shape="rect" coords="204,283,267,318" href="#"> 
</map> 

$(document).ready(function(){ 
    $(".map").click(function() { 
     alert("Click: " + this.id); 
    }); 
}); 

感谢

解决

我添加类区域标记。

<area class="qwe" shape="circle" id="vrsac" coords="317,191,25" href="#">  

回答

1

你有两个选择这里。第一个是使用正确的选择器:你想跟踪area标签的点击,而不是跟map类的某些元素。所以,你的代码应该是:

$("area").click(function() { 
    alert("Click: " + this.id); 
}); 

或第二,更有效的在性能方面 - 从委托内area标签单击事件于母公司map

$("map").on('click', 'area', function() { 
    alert("Click: " + this.id); 
}); 
0

尝试将您的jQuery选择从

$更改( “.MAP”) 到 $( “区域”)

相关问题