单击时更改背景图像
当用户单击链接时,我想更改水平菜单的背景图像。我只是遇到了一点麻烦,我的代码如下:
<div class="grid_16" id="menu">
<ul id='menuTab' class='sqglDropMenu'>
<li><a href='Home' onclick='changeIt();'>Home</a></li>
<li><a href='profile' onclick='changeIt();'>Profile</a></li>
<li><a href='products' onclick='changeIt();'>Products</a></li>
<li><a href='samples' onclick='changeIt();'>Samples</a></li>
<li><a href='contact' onclick='changeIt();'>Contact</a></li>
</ul>
</div>
我的 JS 看起来像这样:
function changeIt(){
document.getElementById('menuTab').style.backgroundImage = "url(../images/tab_off.png)";
}
我的 CSS,其中设置了初始背景图像:
.sqglDropMenu li {float: left; list-style: none; text-align:left; margin-left:5px; position: relative; display: block; line-height: 30px;}
.sqglDropMenu li a {display: block; white-space: nowrap; float: left; text-align:center; font-size: 1.3em; color: #fff; font-weight:bold; padding-top:5px; margin-top:-1px; background:url(../images/menu_tab.png) no-repeat; width:182px; height:58px;}
.sqglDropMenu li ul {float:left; position: absolute; visibility: hidden; z-index:1000; margin: 0; margin-top:50px; * margin-top: 0px; clear:both; margin-left:-475px; background:url(../images/menuback.png) no-repeat; width:944px; height:60px}
.sqglDropMenu li ul li{float: left; position: relative; clear: left; border-width:0px; display: inline; margin: 0;}
.sqglDropMenu li ul li a{margin-left:20px; border-width:0px; text-align:left; font: 9pt Arial; adding:5px 12px; z-index:100; width: 100px; background:none;}
.sqglDropMenu li ul li a:hover{border-width:0px; background:none;}
感谢任何帮助....问候,斯蒂芬
I'm want to change the background image of my horizontal menu when the user clicks on a link. I'm just having a bit of trouble, my code is as follows:
<div class="grid_16" id="menu">
<ul id='menuTab' class='sqglDropMenu'>
<li><a href='Home' onclick='changeIt();'>Home</a></li>
<li><a href='profile' onclick='changeIt();'>Profile</a></li>
<li><a href='products' onclick='changeIt();'>Products</a></li>
<li><a href='samples' onclick='changeIt();'>Samples</a></li>
<li><a href='contact' onclick='changeIt();'>Contact</a></li>
</ul>
</div>
My JS looks like this:
function changeIt(){
document.getElementById('menuTab').style.backgroundImage = "url(../images/tab_off.png)";
}
My CSS, where the initial background image is set:
.sqglDropMenu li {float: left; list-style: none; text-align:left; margin-left:5px; position: relative; display: block; line-height: 30px;}
.sqglDropMenu li a {display: block; white-space: nowrap; float: left; text-align:center; font-size: 1.3em; color: #fff; font-weight:bold; padding-top:5px; margin-top:-1px; background:url(../images/menu_tab.png) no-repeat; width:182px; height:58px;}
.sqglDropMenu li ul {float:left; position: absolute; visibility: hidden; z-index:1000; margin: 0; margin-top:50px; * margin-top: 0px; clear:both; margin-left:-475px; background:url(../images/menuback.png) no-repeat; width:944px; height:60px}
.sqglDropMenu li ul li{float: left; position: relative; clear: left; border-width:0px; display: inline; margin: 0;}
.sqglDropMenu li ul li a{margin-left:20px; border-width:0px; text-align:left; font: 9pt Arial; adding:5px 12px; z-index:100; width: 100px; background:none;}
.sqglDropMenu li ul li a:hover{border-width:0px; background:none;}
Any help appreciated....regards, Stephen
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当您单击链接时,您将被重定向到选定的页面,因此您的功能将毫无用处,您可以使用
var path = window.location.pathname;
var page = path.substring(path.lastIndexOf('/') + 1);
if (page == 'currentpage.html'){
document.write('当前页')
} 别的 {
document.write('当前页')
帮助
我希望这对你有
when you click on the link, you are redirected to the selected page so your function will be useless, you can do what you want either using
var path = window.location.pathname;
var page = path.substring(path.lastIndexOf('/') + 1);
if (page == 'currentpage.html'){
document.write('CurrentPage')
} else {
document.write('CurrentPage')
}
I hope this helps you
试试这个代码
Try this code
您的浏览器将重定向到
href
属性中的值,即您的链接名称。使用changeIt();而是在onclick
处理程序中返回 false。Your browser will get redirected to the value in the
href
attribute, which is the name of the link for you. UsechangeIt(); return false
in theonclick
handlers instead.尝试设置
background
属性:可能是
阻止背景可见。您可以将此样式直接应用到样式表中并看看会发生什么吗?
Try setting the
background
property:It could be that the
<li>
s are preventing the background from being visible. Can you apply this style directly into the stylesheet and see what happens?