javascript on click事件在img标记上无法正常工作

javascript on click事件在img标记上无法正常工作,javascript,asp.net,Javascript,Asp.net,我正在尝试使用下面的代码打开图像点击链接 <img src="AllScripts/SliderImages/images/1Main.png" alt="" title="" onclick="openSliderImage(id);" onmouseover="this.style.cursor='hand'" width="800px" height="300px" id="http://www.google.com" /> 下面的代码用于编写html <% {

我正在尝试使用下面的代码打开图像点击链接

 <img src="AllScripts/SliderImages/images/1Main.png" alt="" title="" onclick="openSliderImage(id);"  onmouseover="this.style.cursor='hand'"  width="800px" height="300px" id="http://www.google.com" />
下面的代码用于编写html

 <% {

                               Response.Write(SlidingImages);
                       }

                    %>
更改id(因为您引用的是一个名为id的javascript变量,所以您希望以图像id为目标,然后使用
this.id

<img src="AllScripts/SliderImages/images/1Main.png" alt="" title="" onclick="openSliderImage(this.id);"  onmouseover="this.style.cursor='hand'"  width="800px" height="300px" id="http://www.google.com" />


我还想说,您可以删除onmouseover并使用CSS(如果使用IE7+)设置它img:hover(或者设置一个cssClass,以便不是所有图像都会成为目标)。

使用这样的ID打开链接看起来像是一种黑客行为。我只会将url作为参数直接输出到函数中,以喜欢它必须打开的链接

<img id="googleImg" onclick="openSliderImage('http://www.google.com');" src="AllScripts/SliderImages/images/1Main.png" alt="" title=""   onmouseover="this.style.cursor='hand'"  width="800px" height="300px" />


您的代码没有运行,因为它将查找名为“id”的javascript变量。

在问题中也发布您的javascript函数。这将让您了解内部情况。根据我的经验,生成hmtl时出现一些错误,因为单击图像时不会触发警报,但它会打开新选项卡并重定向到另一个u我在questionAlert中的rl没有被触发,但在新选项卡中打开链接是这张图像在
标记中吗?检查是否有其他图像点击处理程序。@user1387147对我有用:标记是否有效,所以图像周围没有-element或其他东西?@user1387147没有实际显示问题的标记很难给出正确的答案。
<img src="AllScripts/SliderImages/images/1Main.png" alt="" title="" onclick="openSliderImage(this.id);"  onmouseover="this.style.cursor='hand'"  width="800px" height="300px" id="http://www.google.com" />
<img id="googleImg" onclick="openSliderImage('http://www.google.com');" src="AllScripts/SliderImages/images/1Main.png" alt="" title=""   onmouseover="this.style.cursor='hand'"  width="800px" height="300px" />