我有一个文档,我在其中为我的 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");
...工作正常!