简体   繁体   English

如何将类添加到div中的当前活动div

[英]How to addclass to current active div in div

I have few similar blocks with similar content. 我有几个类似内容相似的块。 If I hover on first block , for first block and one other sub-block in this first parent block classes should be added. 如果我将鼠标悬停在第一个块上 ,则应该添加第一个块第一个父块中的另一个子块 And for the second block too: if I hover on second block for second block and sub-block in this parent second block shold be added the same classes. 并且对于第二个块也是如此:如果我将第二个块悬停在第二 个块上并且在该父第二个块中的 子块中添加相同的类。

Actually I can do unique classes or indentify for each block seperatly but I this this isn't correct and if I want to add I more block I have to change css, html and javascript code - this is uncomfortable. 实际上我可以单独为每个块做独特的类或识别,但我这是不正确的,如果我想添加更多块我必须更改css,html和javascript代码 - 这是不舒服的。

 var Block1 = $('.point-to-black'); ShopBlock1.mouseover(function(){ $(this).addClass('c-lb-black'); $('.c-lb-text-zone').addClass('c-lb-tz-full'); }); ShopBlock1.mouseout(function(){ $(this).removeClass('c-lb-black'); $('.c-lb-text-zone').removeClass('c-lb-tz-full'); }); 
 .point-to-black { width: 100%; height: 100px; transition: 0.5s; } .c-lb-black{ background-color: rgba(0,0,0,0.6)!important; width: 100%!important; height: 100px!important; transition: 0.5s; } .c-lb-tz-full { transition: 0.3s; margin: 0px 0px 0px 33%!important; padding: 3px 25%!important; font-size: 25px!important; transform: skew(-20deg)!important; background: #c1af25!important; line-height: 33px!important; } .c-lb-text-zone1 { line-height: 33px; letter-spacing: 2px; transition: 0.3s; margin: 0px 0px 0px 50%; padding: 3px 15%; font-size: 25px; transform: skew(-20deg); background: #c1af25; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <ul class="c-link-list"> <li> <div class="c-lb-blog"> <div class="point-to-black"> <div class="c-lb-text-zone"> <h1>Blog1</h1> </div> </div> </div> </li> <li> <div class="c-lb-blog"> <div class="point-to-black"> <div class="c-lb-text-zone"> <h1>Blog2</h1> </div> </div> </div> </li> </ul> 

So if I do mouseover on 'point-to-black' block 'c-lb-black' class adds good just for current block because of 'this' pointer but 'c-lb-tz-full' adds for all blocks with class 'c-lb-text-zone' . 因此,如果我在'point-to-black'块上进行鼠标悬停'c-lb-black'类为当前块增加了好处,因为'this'指针但是'c-lb-tz-full'为所有块添加了类'c-lb-text-zone'

$(this).find('.c-lb-text-zone').addClass('c-lb-tz-full');

And

$(this).find('.c-lb-text-zone').removeClass('c-lb-tz-full');

It will affect only the children of the specific div. 它只会影响特定div的孩子。

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

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