我是js的初学者,在练习的过程中遇到了一个没能解决的问题,就是当鼠标移动到菜单弹出选项卡里的时候没法实现改变其当前选项卡背景颜色的功能,代码如下:<!doctype html>
<html>
<head>
<meta charset="utf-8">
<link id="link0" type="text/css" rel="stylesheet" href="ifelse.css" />
<script type="text/javascript" src="ifelse.js"></script>
<title>无标题文档</title>
</head><body id="body">
<div id="table">
<div id="kuang1">
<p><a href="#" id="btn">下拉菜单</a></p>
<ul id="ul1" >
<li><a href="#" class="a1">下拉选项1</a></li>
<li><a href="#" class="a1">下拉选项2</a></li>
<li><a href="#" class="a1">下拉选项3</a></li>
<li><a href="#" class="a1">下拉选项4</a></li>
<li><a href="#" class="a1">下拉选项5</a></li>
</ul>
</div>
</div>
</body>
</html>// JavaScript Document
window.onload=function (){
var Btn=document.getElementById('btn');
var Ul=document.getElementById('ul1');
var A=document.getElementsByClassName('a1');
Btn.onclick=function ()
{
if(Ul.style.display=='block'){
Ul.style.display='none';
}
else{
Ul.style.display='block';
}
}
Btn.onmouseover=function ()
{
Btn.style.backgroundColor='rgba(0,90,0,1)';
Btn.style.color='rgba(240,240,240,1)';
}
Btn.onmouseout=function ()
{
Btn.style.backgroundColor='rgba(51,51,51,1)';
Btn.style.color='rgba(255,255,255,1)';
}
var i=0;//循环标记变量
for(i=0;i<=A.length;i++)
{
A[i].onmouseover=function ()
{//创建鼠标移过事件
for(i=0;i<=A.length;i++)
{
A[i].style.backgroundColor='rgba(51,51,51,1)';
}//使全部下拉选项背景色统一
this.style.backgroundColor='rgba(0,90,0,1)';
}//将选中的下拉选项框颜色改变
}
}
<html>
<head>
<meta charset="utf-8">
<link id="link0" type="text/css" rel="stylesheet" href="ifelse.css" />
<script type="text/javascript" src="ifelse.js"></script>
<title>无标题文档</title>
</head><body id="body">
<div id="table">
<div id="kuang1">
<p><a href="#" id="btn">下拉菜单</a></p>
<ul id="ul1" >
<li><a href="#" class="a1">下拉选项1</a></li>
<li><a href="#" class="a1">下拉选项2</a></li>
<li><a href="#" class="a1">下拉选项3</a></li>
<li><a href="#" class="a1">下拉选项4</a></li>
<li><a href="#" class="a1">下拉选项5</a></li>
</ul>
</div>
</div>
</body>
</html>// JavaScript Document
window.onload=function (){
var Btn=document.getElementById('btn');
var Ul=document.getElementById('ul1');
var A=document.getElementsByClassName('a1');
Btn.onclick=function ()
{
if(Ul.style.display=='block'){
Ul.style.display='none';
}
else{
Ul.style.display='block';
}
}
Btn.onmouseover=function ()
{
Btn.style.backgroundColor='rgba(0,90,0,1)';
Btn.style.color='rgba(240,240,240,1)';
}
Btn.onmouseout=function ()
{
Btn.style.backgroundColor='rgba(51,51,51,1)';
Btn.style.color='rgba(255,255,255,1)';
}
var i=0;//循环标记变量
for(i=0;i<=A.length;i++)
{
A[i].onmouseover=function ()
{//创建鼠标移过事件
for(i=0;i<=A.length;i++)
{
A[i].style.backgroundColor='rgba(51,51,51,1)';
}//使全部下拉选项背景色统一
this.style.backgroundColor='rgba(0,90,0,1)';
}//将选中的下拉选项框颜色改变
}
}
解决方案 »
- 有没JS高手帮忙看下JS如何写。
- jquery写的一个图片切换效果,图片显示不正常,请大侠指点一下。
- 各位帮帮忙啊!!!!!我没辙了......jq和js的问题
- firefox input onclick无效,求解决
- IE6的option如何实现title功能
- JavaScript小问题
- iframe 自动适应嵌套的页面大小,不会出现滚动条
- asp网页提交表单问题,请指正!多谢!
- 为什么不显示 替换后的内容???
- 请教设置什么参数使showModalDialog('http://design.xuse.net')打开的窗口最大化?急!!!
- 为什么不能实现侧边栏和主体内容跳转啊 求大侠指导一下
- 大虾指点function(key,value)是啥?
其二:你如果想改变当前鼠标移入的这个选项,那么你就不要循环,直接利用this这个对象就可以了,首先将所有的选项恢复到初始状态,然后this.style.........='rgba()';这样就行了 还有问题可以给我发站内信