onclick事件不'不要在萤火虫或铬中点火

onclick event doesn't fire in firefox or chrome

本文关键字:萤火虫 事件 onclick      更新时间:2023-09-26

我正在用jQuery编写一些javascript,以便在我的asp.net页面中单击img时激发。

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中则不然,这让我认为有一些简单的错误,我无法发现

您的页面包含两个名称相同的元素''id:IMG HTML元素,id属性为"zoomIn",javascriptfunction名称为zoomIn

尝试重命名function zoomInIMG#zoomIn

既然您已经使用了jQuery,那么您只需要使用

$('#zoomIn').click(zoomIn);

将其包装在锚点标记中,并相应地设置样式。

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

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