简体   繁体   English

仅将CSS应用于子DIV

[英]Apply CSS to child DIV only

OK, let's say I have a similar structure like the one following : 好的,假设我有一个类似以下结构的结构:

<div comp-id='one'>
    <div comp-id='two'>
         <div comp-id='three'>
         ...
         </div>
    </div>
</div>

All I want to do is to apply a hover effect to the child (in this case, the one with comp-id="three" ). 我要做的就是对孩子应用悬停效果(在本例中为comp-id="three" )。


IMPORTANT NOTE : I don't want to target specifically [comp-id="three"] . 重要说明:我不想专门定位 [comp-id="three"] I want to target any child, with no-matter-which comp-id value. 我想针对任何没有comp-id值的孩子。 In the real-world example, there may be infinite nested [comp-id] ed div s. 在实际示例中,可能存在无限嵌套的[comp-id] ed div I just need the innermost child. 我只需要最里面的孩子。

What I'm trying to achieve : 我想要达到的目标:

  • User hovers over three , then just three is highlighted (not one and two ) 用户将鼠标悬停three ,则只需 three高亮显示(没有onetwo
  • User hovers over two , then just two is highlighted (not one - and, of course, not three ) 用户将鼠标悬停two ,然后只需 two高亮显示(没有one -并且,当然,不是three

My current CSS code : 我当前的CSS代码:

[comp-id]:hover {
    box-shadow: inset 0 0 0 2px red;
    -webkit-box-shadow: inset 0 0 0 2px red;
    -moz-box-shadow: inset 0 0 0 2px red;
    -o-box-shadow: inset 0 0 0 2px red;
}

However, my current CSS highlights everything which is not what I want. 但是,我当前的CSS突出显示了我不需要的所有内容。 I could think of a javascript-based solution. 我可以想到一个基于JavaScript的解决方案。 What about CSS? 那CSS呢?


Warning : I'm not such a guru with CSS, so this may be really simple... :-) 警告:我不是CSS专家,所以这可能真的很简单... :-)

And... well... thanks to @BMH, this is what worked : 而且...好吧...多亏@BMH, 这才有效:

$('[comp-id]:not(.hover)').mousemove(function(e){
    $('[comp-id]').removeClass('hover');
    e.stopPropagation();
    $(this).addClass('hover');

});

This is a jQuery solution: 这是一个jQuery解决方案:

$('[comp-id]').mousemove(function(e){
    $(this).removeClass('hover');
    $(e.target).addClass('hover'); 
}).mouseleave(function(e){
    $(this).removeClass('hover');
});

http://jsfiddle.net/sMRMz/4/ http://jsfiddle.net/sMRMz/4/

Try something like this in jQuery: 在jQuery中尝试类似的方法:

CSS: CSS:

.hoverableClass:hover {
    box-shadow: inset 0 0 0 2px red;
    -webkit-box-shadow: inset 0 0 0 2px red;
    -moz-box-shadow: inset 0 0 0 2px red;
    -o-box-shadow: inset 0 0 0 2px red;
}

JS: JS:

function deepSearch(parent){
    var b_ClassAdd = true;
    if (!parent){
        parent = $('body');
        b_ClassAdd = false;
    }

    var allDivChilds = parent.find('div[comp-id]');
    if ((!allDivChilds || allDivChilds.length == 0) && b_ClassAdd)
        parent.addClass('hoverableClass'); 
    else
        for (var i = 0; i < allDivChilds.length; i++)
            deepSearch($(allDivChilds[i]));
}

deepSearch();

This code is untested and just an idea for a solution. 此代码未经测试,仅是解决方案的想法。

You can use '>' child selector for this,to stop the cascading styles 您可以使用'>'子选择器来停止级联样式

[comp-id]:hover {
    box-shadow: inset 0 0 0 2px red;
    -webkit-box-shadow: inset 0 0 0 2px red;
   -moz-box-shadow: inset 0 0 0 2px red;
    -o-box-shadow: inset 0 0 0 2px red;
}

[comp-id]:hover > *{
  box-shadow: none;
    -webkit-box-shadow: none;
   -moz-box-shadow: none;
    -o-box-shadow: none;
}

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

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