我创建了一个div和一个锚列表。
<div id="wrap">
<div id="box1"></div>
<div id="box2"></div>
<ul>
<li><a href=".." id="a1">text1</a></li>
<li><a href=".." id="a2">text2</a></li>
</ul>
</div>
我的目标是,当我鼠标悬停在box1
时,a1
将更改文字颜色,当鼠标移出时,它将恢复为正常颜色。
我尝试使用javascript进行此操作,但问题是当我将鼠标悬停在其中一个div框上时,例如我鼠标悬停在box1
上a1
会将颜色更改为红色但是当我mouseout,它不会变为蓝色,它将保持红色。
这个mycode看起来像:
<script type="text/javascript">
function mouseoverBox1(){
var myPara = document.getElementById("a1");
myPara.style.color = "red";
}
function onmouseoutBox1(){
var myPara = document.getElementById("a1");
myPara.style.color = "blue";
}
function mouseoverBox21(){
var myPara = document.getElementById("a2");
myPara.style.color = "red";
}
function onmouseoutBox2(){
var myPara = document.getElementById("a2");
myPara.style.color = "blue";
}
</script>
<style>
a{color:red;}
a:hover{color:blue;}
.box{min-height: 180px;width: 220px;position: absolute;cursor: pointer;}
</style>
<div id="wrap">
<div class="box" id="box1" onmouseover="mouseoverBox1()" onmouseout="onmouseoutBox1()" ></div>
<div class="box" id="box2" onmouseover="mouseoverBox2()" onmouseout="onmouseoutBox2()"></div>
<ul>
<li><a href=".." id="a1">text1</a></li>
<li><a href=".." id="a2">text2</a></li>
</ul>
</div>
有没有人对我的案子有所了解?
任何帮助将不胜感激。感谢
答案 0 :(得分:2)
<div id="box2" onmouseover="mouseoverBox2()" onmouseout="onmouseoutBox2()" "></div>
这一行有一个额外的“。应该是:
<div id="box2" onmouseover="mouseoverBox2()" onmouseout="onmouseoutBox2()"></div>
答案 1 :(得分:1)
答案 2 :(得分:1)
<script type="text/javascript">
function mouseoverBox1(){
var myPara = document.getElementById("a1");
myPara.style.color = "red";
}
function onmouseoutBox1(){
var myPara = document.getElementById("a1");
myPara.style.color = "blue";
}
function mouseoverBox2(){
var myPara = document.getElementById("a2");
myPara.style.color = "red";
}
function onmouseoutBox2(){
var myPara = document.getElementById("a2");
myPara.style.color = "blue";
}
</script>
<style>
a{color:red;}
a:hover{color:blue;}
</style>
<div id="wrap">
<div id="box1" onmouseover="mouseoverBox1()" onmouseout="onmouseoutBox1()">deve</div>
<div id="box2" onmouseover="mouseoverBox2()" onmouseout="onmouseoutBox2()">deve</div>
<ul>
<li><a href="#" id="a1">text1</a></li>
<li><a href="#" id="a2">text2</a></li>
</ul>
</div>
你拼错了一个类名,并在div标签的末尾添加了一个引号。
如果您没有经验编写代码,这种错误很常见;
答案 3 :(得分:0)
.................
您现在习惯于查询 Live demo
$(document).ready(function() {
$("#box1").mouseenter(function(){
$("#a1").addClass("a1_hover");
});
$("#box1").mouseleave(function(){
$("#a1").removeClass("a1_hover");
$("#a1").addClass("a1")
});
$("#box2").mouseenter(function(){
$("#a2").addClass("a2_hover");
});
$("#box2").mouseleave(function(){
$("#a2").removeClass("a2_hover");
$("#a2").addClass("a2")
});
});
答案 4 :(得分:0)
尝试使用css:
<style type="text/css">
#a1 { color:blue; }
#box1:hover + #a1 { color:red; }
</style>
<div id="box1">Color text 1 to red</div>
<a href="" id="a1">Text1</a>
请参阅DEMO