0

我有一个文档,我在其中为我的 javascript 编写了这行代码:

<script type="text/javascript"> function OnMouseOverFunction(id) {


                     var value = id.id;

                     alert("welcome");



                     element1 = document.getElementsByName("Image1");
                     element2 = document.getElementById("Image2");
                     element3 = document.getElementById("Image3");
                     element4 = document.getElementById("Image4");
                     element5 = document.getElementById("Image5");


                     if (value == "ContentPlaceHolder1_Image1") {
                         id.src = "images/zvezda3.png";
                     } else if (value == "ContentPlaceHolder1_Image2") {

                         id.src = "images/zvezda3.png";
                         element1.src = "images/zvezda3.png";

                     }



                 }</script>

这些是下面的图片:

<b>Ocena:</b>
                 <asp:Image onmouseover="OnMouseOverFunction(this)" onmouseout="mouseout(this)" name="1" ImageUrl="images/zvezda2.png" id="Image1" runat="server" /> 
                 <asp:Image onmouseover="OnMouseOverFunction(this)" onmouseout="mouseout(this)" value="2" ImageUrl="images/zvezda2.png" id="Image2" runat="server" />
                 <asp:Image onmouseover="OnMouseOverFunction(this)" onmouseout="mouseout(this)" value="3" ImageUrl="images/zvezda2.png" id="Image3" runat="server" />
                 <asp:Image onmouseover="OnMouseOverFunction(this)" onmouseout="mouseout(this)" value="4" ImageUrl="images/zvezda2.png" id="Image4" runat="server" />
                 <asp:Image onmouseover="OnMouseOverFunction(this)" onmouseout="mouseout(this)" value="5" ImageUrl="images/zvezda2.png" id="Image5" runat="server" />

如果我删除警报(“欢迎”);图片变了。但是有了警报,我收到了这个错误:

Property 'alert' of object [object Object] is not a function

如果鼠标位置在 3.image 上,我的目标是更改 3 种颜色(变为黄色图片)。等等...但是为什么警报不起作用?我也想知道,我怎样才能通过它的 ID 获取元素?

element2 = document.getElementById("Image2"); 
element2.id //it returns null

编辑:

但是...

document.write("test");

...工作正常!

4

1 回答 1

0

服务器控件上的 'id' 属性仅在服务器上有效,.net 在发出 HTML 时会破坏 id。

要解决此问题,您需要获取 ClientId 以在 JavaScript 中使用。

尝试 document.getElementById('<%= Image5.ClientId %>');

替换每个图像的服务器 ID

于 2013-06-18T13:25:15.510 回答