2009-12-18 114 views
0

假設您有兩張圖像使用相同的圖像地圖。如何在圖像地圖鼠標中獲取圖像的ID

<img src="/test/image1.jpg" id="image1" useMap="map-name"> 
<img src="/test/image2.jpg" id="image2" useMap="map-name"> 

<map name="map-name"> 
    <area shape="rect" coords="0,0,82,126" alt="Sun" onmouseover="testFunction();"/> 
    <area shape="circle" coords="90,58,3" alt="Mercury" onmouseover="testFunction();"/> 
    <area shape="circle" coords="124,58,8" alt="Venus" onmouseover="testFunction();"/> 
</map> 

有沒有什麼辦法讓圖像(圖像1 |圖像2在這種情況下)的ID testFunction內()?

回答

0

是的。最簡單的方法是使用像jQuery這樣的庫來連接事件,而不是使用onmouseover。如果你這樣做,你基本上做這樣的事情:

$("AREA").mouseOver(function (e) { 
    var id = $(e.target).attr("id"); 
} 

如果你不這樣做,那麼......好吧玩弄「這個」你testFunction的定義,和好運氣內(因爲你將會處理跨瀏覽器不兼容的喜悅)。

+0

-1你試過這個解決方案嗎? #1在jQuery和DOM Event對象本身中都沒有'getTarget()'。 – jitter 2009-12-19 11:23:11

+0

沒有getTarget(),但事件對象中有e.target,e.currentTarget和e.relatedTarget屬性。這是你指的是什麼?如果是,那些屬性給出不穩定的結果 - 有時它返回的ID和其他時間它返回圖像的ID。 任何人有任何其他建議,或者你可以澄清你要去機槍?感謝您的原始答覆。 – user234882 2009-12-19 21:49:02

+0

哎呦;不知道我從哪裏得到getTarget()...... *嘆息* ...但是,是的,e.target和e.currentTarget就是我的意思。我會解決答案。至於不一致,這似乎很奇怪。你可以識別任何模式,以便當它返回圖像映射的ID與區域? – machineghost 2009-12-19 22:20:16