简体   繁体   English

在子级点击时从父类删除类-jQuery

[英]Remove class from parent on child click - jQuery

I am completely stuck on this problem ( see jsfiddle here ). 我完全陷入了这个问题( 请参阅jsfiddle此处 )。 I have a very simple set up where an onclick adds a class which displays an element. 我有一个非常简单的设置,其中onclick添加了一个显示元素的类。 Within this element is a close button for which the onclick should remove the class from the parent. 在此元素内有一个关闭按钮, onclick应当从父按钮中删除该类。 But this is not working. 但这是行不通的。

I have tried removeAttr('class') which also fails. 我试过了removeAttr('class') ,它也失败了。 Looking in chrome inspector I can see the selector is right and is targeting the parent, but the class cannot be removed. 在chrome inspector中查看,我可以看到selector正确且以父级为目标,但是无法删除该类。

This is part of a larger project so I cannot change the html structure. 这是一个较大项目的一部分,因此我无法更改html结构。 But welcome any suggestions on how to do this in jQuery. 但是欢迎有关如何在jQuery中执行此操作的任何建议。

HTML: HTML:

<div class="print-wrap">
    <ul>
        <li class="settings-tab">+</i>
            <div class="settings-wrap">
                <div class="iphone-close">x</div>
                <div class="content"></div>
            </div>
        </li>
        <li class="img">
            <img src="http://placehold.it/250x166">
        </li>
    </ul>
</div>

jQuery: jQuery的:

jQuery(function(){
    jQuery('.settings-tab').click(function(){
        var $this = jQuery(this);
        $this.addClass('settings-out');
    });

    jQuery('.settings-wrap .iphone-close').click(function(e){
        var $this = jQuery(this);
        var $parent = $this.parent('.settings-wrap').parent('.settings-tab');
        $parent.removeClass('settings-out');
    });
});

Use event.stopPropagation in the event handler of close button. 在关闭按钮的事件处理程序中使用event.stopPropagation The event is bubbled up to the parent .settings-tab and the handler of is is executed which is adding the class again. 事件冒泡到父.settings-tab并执行的处理程序,该处理程序再次添加了该类。

Updated Fiddle 更新小提琴

 jQuery(function() { //settings tab: jQuery('.settings-tab').click(function() { jQuery(this).addClass('settings-out'); }); //Settings tab close jQuery('.settings-wrap .iphone-close').click(function(e) { jQuery(this).closest('.settings-out').removeClass('settings-out'); e.stopPropagation(); }); }); 
 .print-wrap { width: 250px; height: 250px; overflow: hidden; position: relative; display: block; margin: 0 auto; margin-bottom: 20px; background-color: #D6A0A0; } .print-wrap ul { list-style: none; margin: 0; padding: 0; height: 100%; position: relative; left: 0; } .settings-tab { width: 30px; height: 30px; position: absolute; top: 0; right: 0; text-align: center; background: #f5d43f; transition: all .3s ease-in-out; transition-delay: .25s; z-index: 4; cursor: pointer; } .settings-tab .settings-wrap { width: 250px; right: -250px; height: 250px; background: lightblue; position: absolute; top: 0; } .settings-out { right: 250px; } .iphone-close { background: pink; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="print-wrap"> <ul> <li class="settings-tab">+</i> <div class="settings-wrap"> <div class="iphone-close">x</div> <div class="content"></div> </div> </li> <li class="img"> <img src="http://placehold.it/250x166"> </li> </ul> </div> 

jQuery(document).on('click','.settings-wrap .iphone-close',function(e){
         var $this = jQuery(this);
        var $parent = $this.parent('.settings-wrap').parent('.settings-tab');
        $parent.removeClass('settings-out');
    });
});

try changing jQuery(document).on('click','.settings-wrap .iphone-close',function(e){ 尝试更改jQuery(document).on('click','.settings-wrap .iphone-close',function(e){

demo 演示

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

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