2015-04-07 39 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」) 到 $( 「區域」)

相關問題