简体   繁体   中英

On click of child div having same class as its parent some class should be added to child div not parent div and vice versa

I have following html structure.

<div class="someclass">
    <div class="someclass"></div>
</div>

So when I click on child div "someclass" some other class say "test" should be added to it but that class should not be added to parent div.someclass and if I click on parent div then "test" class should be added to parent div not to child div

Use .addClass and use this context. stopPropogation will not bubblr the event on parent element.

 $('.someclass').on('click', function(e) { e.stopPropagation(); $('.someclass').removeClass('test'); $(this).addClass('test'); }) 
 .test { color: red!important; } .someclass { color: blue; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <div class="someclass">Parent <div class="someclass">Child</div> </div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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