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

html - Change background colour of parent div on child hover in javascript? - Stack Overflow

programmeradmin2浏览0评论

I have three coloured boxes in a div, all of different colors, and when i hover upon any of these boxes i have to make the background-color of the parent div appear with the same color as the inner-box which is being hovered upon.

CSS:

 .t1_colors {
    float: left;
    width: 100px;
    height: 100px;
    margin-right: 20px;
    border: 1px solid rgb(111,61,69);
    }

HTML:

<div id="task1" class="task">
    <h2>Task 1</h2>
    <p>Change the background color, of the div that     contains this task, to the color in each box when the box is hovered over.</p>
    <p>When the mouse stops hovering over the box, change the background color back to white.</p>
    <div id="t1_color_one" class="t1_colors" style="background: goldenrod;"></div>
    <div id="t1_color_two" class="t1_colors" style="background: lightgreen;"></div>
    <div id="t1_color_three" class="t1_colors" style="background: palevioletred;"</div>
</div>

Our class is using addEventListener if that makes anything any easier. Thanks in advance for any feedback, and will be greatly appreciated.

I have three coloured boxes in a div, all of different colors, and when i hover upon any of these boxes i have to make the background-color of the parent div appear with the same color as the inner-box which is being hovered upon.

CSS:

 .t1_colors {
    float: left;
    width: 100px;
    height: 100px;
    margin-right: 20px;
    border: 1px solid rgb(111,61,69);
    }

HTML:

<div id="task1" class="task">
    <h2>Task 1</h2>
    <p>Change the background color, of the div that     contains this task, to the color in each box when the box is hovered over.</p>
    <p>When the mouse stops hovering over the box, change the background color back to white.</p>
    <div id="t1_color_one" class="t1_colors" style="background: goldenrod;"></div>
    <div id="t1_color_two" class="t1_colors" style="background: lightgreen;"></div>
    <div id="t1_color_three" class="t1_colors" style="background: palevioletred;"</div>
</div>

Our class is using addEventListener if that makes anything any easier. Thanks in advance for any feedback, and will be greatly appreciated.

Share Improve this question edited Oct 29, 2013 at 2:37 Nightfirecat 11.6k6 gold badges37 silver badges53 bronze badges asked Oct 29, 2013 at 2:17 Jeremy StoneJeremy Stone 3504 gold badges12 silver badges30 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 6

Look in pure JavaScript:

<div>
  <div id="child" onMouseOver="this.parentNode.style.background='red'">Hover Me</div>
</div>

With jQuery:

 $("#child").hover(function(){
     $(this).parent().css("background","red");
 });

UPDATE: not Click, it's Hover. Fixed css property name.

var parent = document.getElementById("task1");//get parent element

var item1 = document.getElementById("t1_color_one");//get child element

item1.addEventListener("mouseover", func, false);//add event listener for first item on mouseover and call func when someone mouse overs it

function func()
{  
  parent.setAttribute("style", item1.getAttribute("style"));//set the style attribute of the parent to the style attribute of the child
}

and then you can do something similar for the rest of them.

Here is answer in pure javascript

window.addEventListener('load', function(event)
{
    var divs = document.getElementsByClassName('t1_colors');
    var count_of_divs = divs.length;

    for(var i = 0; i<count_of_divs; i++)
    {
        divs[i].addEventListener('mouseover', function(e)
        {
            document.getElementById('task1').setAttribute('style', e.target.getAttribute('style'));
        }, false);

        divs[i].addEventListener('mouseout', function(e)
        {
            document.getElementById('task1').removeAttribute('style');
        }, false)
    }
}, false);

And you can check it in jsFiddle link.

Use the following code:

<html>
    <head>
        <style>
            .t1_colors {
            float: left;
            width: 100px;
            height: 100px;
            margin-right: 20px;
            border: 1px solid rgb(111,61,69);
            }
        </style>
    </head>
    <body>
        <div id="task1" class="task">
            <h2>Task 1</h2>
            <p>Change the background color, of the div that     contains this task, to the color in each box when the box is hovered over.</p>
            <p>When the mouse stops hovering over the box, change the background color back to white.</p>
            <div id="t1_color_one" class="t1_colors" style="background: goldenrod;">ugy</div>
            <div id="t1_color_two" class="t1_colors" style="background: lightgreen;">hjk</div>
            <div id="t1_color_three" class="t1_colors" style="background: palevioletred;">jkk</div>
        </div>
    </body>
    <script>
        try
        {
            function change_bgcolor(elem)
            {
                elem.addEventListener('mouseover', function(){elem.parentNode.style.backgroundColor=elem.style.backgroundColor}, false);
            }

            function f1()
            {
                div_arr=document.getElementsByTagName('div');
                for(x in div_arr)
                {
                    if(div_arr[x].parentNode.tagName=="DIV")
                    {
                        change_bgcolor(div_arr[x]);
                    }
                }
            }
        }
        catch(e)
        {alert(e);}
        onload=f1();
    </script>
</html>

Fiddle here

发布评论

评论列表(0)

  1. 暂无评论