为什么在javascript中设置ImageButton的ImageUrl不起作用?(Why is setting ImageUrl of ImageButton in javascript does

编程入门 行业动态 更新时间:2024-10-26 18:22:21
为什么在javascript中设置ImageButton的ImageUrl不起作用?(Why is setting ImageUrl of ImageButton in javascript doesn't work?)

我是asp.net的新手。 我尝试使用以下代码更改asp.net imageButton的图像:

<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="/Images/blueButton.png" Width="137px" Height="34px" onmouseover="this.ImageUrl='/Images/greenButton.png'" onmouseout="this.ImageUrl='/Images/blueButton.png"/>

它没用。 但是,以下代码确实有效:

<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="/Images/blueButton.png" Width="137px" Height="34px" onmouseover="this.src='/Images/greenButton.png'" onmouseout="this.src='/Images/blueButton.png"/>

如您所见,我使用的是src而不是ImageUrl

为什么没有第一个工作?

I'm new to asp.net. I tried to change the image of an asp.net imageButton, using this code:

<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="/Images/blueButton.png" Width="137px" Height="34px" onmouseover="this.ImageUrl='/Images/greenButton.png'" onmouseout="this.ImageUrl='/Images/blueButton.png"/>

It didn't work. However, the following code did work:

<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="/Images/blueButton.png" Width="137px" Height="34px" onmouseover="this.src='/Images/greenButton.png'" onmouseout="this.src='/Images/blueButton.png"/>

As you can see, I was using src instead of ImageUrl.

Why didn't the first one work?

最满意答案

您正在混合服务器和客户端代码。 JavaScript在客户端上运行,而<asp:ImageButton>在服务器上运行。 这是两个独立的域。

ASP.NET在服务器<img src="/Images/blueButton.png"> <asp:ImageButton>呈现为HTML <img src="/Images/blueButton.png">并将其发送到客户端,然后鼠标事件可以访问它的名为src的DOM属性。客户。

查看浏览器中的HTML源代码,了解您在ASPX文件中编写的内容与浏览器收到的HTML之间的区别。

You are mixing server and client code. JavaScript runs on the client, and your <asp:ImageButton> runs on the server. These are two separate domains.

ASP.NET renders the <asp:ImageButton> as an HTML <img src="/Images/blueButton.png"> on the server and sends it to the client, and then the mouse event can access it's DOM property named src at the client.

Look at the HTML source from your browser to see the difference between what you wrote in your ASPX file and the HTML the browser receives.

更多推荐

本文发布于:2023-07-05 14:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1038449.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   ImageButton   javascript   ImageUrl   work

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!