我在asp.net项目的母版页中使用了以下代码
<a runat="server" href="home.htm" onmouseover="document.Home_Img.src='Images/home_2.png'"
onmouseout="document.Home_Img.src='Images/home.png'">
<img alt="" src="Images/home.png" name="Home_Img" runat="server" />
</a>
它适用于根目录中的所有关联webforms,但不适用于子目录页面。 任何sugesstion都会得到尊重。
Thnx提前......
答案 0 :(得分:6)
您使用相对网址Images/home.png
作为图片位置,将其更改为绝对/Images/home.png
:
<a runat="server" href="home.htm" onmouseover="document.Home_Img.src='/Images/home_2.png'"
onmouseout="document.Home_Img.src='/Images/home.png'">
<img alt="" src="/Images/home.png" name="Home_Img" runat="server" />
</a>
此外,这与CSS无关,因为您使用的是旧式Javascript鼠标悬停。我强烈建议您将其更改为:
<style>
a.home {
background: url(/Images/home.png) no-repeat;
display: block; /* this may not be correct, depends on the layout */
height: 100px; /* height of image */
text-indent: 9999px;
width: 100px; /* width of image */
}
a.home:hover {
background: url(/Images/home_2.png) no-repeat;
}
</style>
<a class="home" href="home.htm">Home</a>
答案 1 :(得分:2)
将您的包装的URL放在ResolveUrl中,如下所示:
<a runat="server" href="<%= this.ResolveUrl("home.htm") %>" onmouseover="document.Home_Img.src='<%= this.ResolveUrl("Images/home_2.png") %>'"
onmouseout="document.Home_Img.src='<%= this.ResolveUrl("Images/home.png") %>'">
<img alt="" src="Images/home.png" name="Home_Img" runat="server" />
</a>