简体   繁体   English

当将鼠标悬停在子节点上时,如何禁用父级悬停

[英]How to disable parent hover, when hovering over child

I will try to show my problem on some simple example. 我将尝试在一些简单的例子中展示我的问题。

What I have? 我有的?
http://jsfiddle.net/JGzSh/3/ http://jsfiddle.net/JGzSh/3/
Here is some simple button, which will have onlick events later. 这是一个简单的按钮,稍后会有onlick事件。 When i hover over green div (parent), :hover works changing its color a bit. 当我将鼠标悬停在绿色div(父级)上时:hover稍微改变其颜色。

What's the problem? 有什么问题?
The problem is, when I hover over yellow part, i want to change background of yellow element, but i don't want to parent :hover to work. 问题是,当我将鼠标悬停在黄色部分上时,我想要更改黄色元素的背景,但我不想让父母:hover上班。

Question
So how can i disable parent :hover when hovering over child? 那么我怎么能禁用父:hover在孩子身上? (so parent's background will go back to starting color?) (所以父母的背景会回到起始色?)

Those are only css rules about hovering so far 那些只是关于徘徊到目前为止的css规则

.przycisk:hover{
    background-color: #383;
}
.skrot:hover{
    background-color: red;
}

What have I tried so far? 到目前为止我尝试了什么?
Thats most important question, I know. 这是最重要的问题,我知道。 I did some research, and so far i found some solutions which could help me, but they all use jQuery, and my question is, if its possible to do it in CSS only , to keep it as simple as possible? 我做了一些研究,到目前为止我找到了一些可以帮助我的解决方案,但他们都使用jQuery,而我的问题是, 如果它只能用CSS做 ,可以保持尽可能简单吗?

Example of solution in jQuery found in google: 在google中找到的jQuery解决方案示例:

$('.przycisk').hover(function(e){
    if($(e.target).is('.skrot')){
        // your child span is being hovered over
        e.stopPropagation();
    }else if($(e.target).is('.przycisk')){
        // your parent element is being hovered over
    }
});

I think i found some pretty quick solution for that. 我想我找到了一些非常快速的解决方案。

$('.deHover').hover(function(){
    $(this).parent().css('background-color', '#008000');
}, function(){
    $(this).parent().css('background-color', '#383');
});

$('.przycisk').hover(function(){
    $(this).css('background-color', '#383');
}, function(){
    $(this).css('background-color', '#008000');
});

To everything what I want to be disabling parent hover, I add deHover class, and it just changes parent background color on mousein/mouseout. 对于我想要禁用父hover的所有内容,我添加deHover类,它只是在mousein / mouseout上更改父背景颜色。 But i also need to remember to make Parent hover (in and out) work, so i added an jQuery for it also. 但是我还需要记住让父素悬停(进出)工作,所以我也为它添加了一个jQuery。

Here is jsfiddle to check if it works. 是jsfiddle来检查它是否有效。 Didn't find anything to complain about. 没有找到任何抱怨的东西。

You might want to see this question , which talks about nesting CSS classes. 您可能希望看到这个问题 ,它讨论了嵌套CSS类。 This might allow you to set .przycisk 's color to it's default when .skrot is being hovered over. 这可能允许您在.skrot悬停时将.przycisk的颜色设置为默认颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM