最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - hover to div to change other element's style - Stack Overflow

programmeradmin0浏览0评论

I create an div and a list of anchor.

<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>

my goal is when I mouseover the box1 the a1 will change the text color and when it mouseout it will back to the normal color.

I have try to do this using javascript but the problem is when I mouseover the one of the div box, for example I mouseover the box1 the a1 will change the color to red but when I mouseout, it will not change to blue, it will remain in red.

this mycode look like:

<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>

does anyone have an idea about my case?

any help will be appreciated. Thanks

I create an div and a list of anchor.

<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>

my goal is when I mouseover the box1 the a1 will change the text color and when it mouseout it will back to the normal color.

I have try to do this using javascript but the problem is when I mouseover the one of the div box, for example I mouseover the box1 the a1 will change the color to red but when I mouseout, it will not change to blue, it will remain in red.

this mycode look like:

<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>

does anyone have an idea about my case?

any help will be appreciated. Thanks

Share Improve this question edited Oct 19, 2012 at 7:25 gadss asked Oct 19, 2012 at 7:11 gadssgadss 22.5k42 gold badges108 silver badges158 bronze badges 4
  • 1 it is working fine I tried on jsfiddle – Abubakkar Commented Oct 19, 2012 at 7:16
  • At least in Google Chrome it is working fine: jsfiddle/KkMtq – j0nes Commented Oct 19, 2012 at 7:17
  • aw... wait, my code is not plete, sorry :) – gadss Commented Oct 19, 2012 at 7:19
  • doing this in css is also possible:stackoverflow./questions/4502633/… – SmithMart Commented Oct 19, 2012 at 7:26
Add a ment  | 

5 Answers 5

Reset to default 2
<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>​​​​​

You mispelled a class name and added a quote at the end of a div tag.

This errors are quite mon if you are not experienced writing code;

<div id="box2" onmouseover="mouseoverBox2()" onmouseout="onmouseoutBox2()" "></div>

This line has an extra " in it. Should be:

<div id="box2" onmouseover="mouseoverBox2()" onmouseout="onmouseoutBox2()"></div>

css

#box1:hover ~ ul #a1 {
  color: red;
}

http://jsfiddle/4tduS/

no js necessary

.................

Hi now used to j query 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")
    });

});

Try using 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>

see DEMO

发布评论

评论列表(0)

  1. 暂无评论