2012-09-06 137 views
0

我使用jQuery編寫了一些javascript,以便在我的asp.net頁面中點擊img時觸發。onclick事件不會在firefox或chrome中觸發

的IMG聲明如下:

<img id="zoomIn" alt="Zoom In" src="/Images/Plus.png" onclick="zoomIn();" /> 

而且.js文件的功能是:

function zoomIn() { 
    zoomLevel += 1; 
    showMapImage(); 
} 

function showMapImage() { 
    //show the location map 
    var img_url = "http://maps.google.com/maps/api/staticmap?center=" + $asp("lblCoordinates").html() + "&size=200x150&sensor=false&zoom=" + zoomLevel + "&markers=color:green%7C" + $asp("lblCoordinates").html(); 
    $("#imgMap").attr('src', img_url); 
} 

function $asp(serverID) { 
    return $("[id$='" + serverID + "']"); 
} 

能正常工作,在IE瀏覽器,但無法在Chrome或Firefox這讓我覺得有一些簡單的錯誤,我無法辨認的地方

+0

你想在zoomIn()函數把剛纔alert(「it works」);並註釋其他行肯定... – dllhell

回答

3

您頁包含兩個元素具有相同名稱\ ID:與ID屬性IMG HTML元素"zoomIn"和javascript function名稱zoomIn

嘗試重命名function zoomInIMG#zoomIn

+0

這是正確答案.. –

+0

這絕對是原因。 – Sherwin

2

將它包裹在錨標籤和樣式中。

<a href="#" onclick="zoomIn();"> 
    <img id="zoomIn" alt="Zoom In" src="/Images/Plus.png" /> 
</a> 

http://jsfiddle.net/H57Vg/5/

+0

'TypeError:zoomIn不是我的FireFug16 Firebug中的函數'爲您的jsfiddle。 –

3

,因爲你已經使用jQuery的,你可以只使用

$('#zoomIn').click(zoomIn); 
相關問題