单击时出现Jquery removeClass()

时间:2014-05-18 13:16:47

标签: javascript jquery html

嗨,我有这个问题:我想点击将翻转的div(这是工作),但下一步是flipp div,点击" x" (这是我的问题)。

HTML code:

<div class="col-md-4">
    <div class="balik">
        <div class="panel panel-default panel-cube">
            <div class=" flip">
                <div class="card">
                    <div class="panel-body face front">
                        <h4>Balík 28 dní</h4>
                        <br>
                        <h1>16,80€</h1>
                        <br>
                        <h4>Zisti viac o balíku &raquo;</h4>
                    </div>
                    <div class="panel-body face back">
                        <p class="close">x</p>  <a href="#"><h4>O Balíku 28 dní</h4></a>
                        &raquo; celkom 28 dní na topovanie
                        <br />&raquo; 5 topovaných inzerátov
                        <br />&raquo; -30% z ceny oproti balíku 7 dní
                        <br />
                        <br>
                        <button class="btn btn-success"><i class="fa fa-shopping-cart"></i> Kúpiť</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

JQUERY代码:

$(function(){
$('.flip').click(function(){
    $(this).find('.card').addClass('flipped')
    });// this is work

$('.close').click(function(){
        $('.flip').find('.card.flipped').removeClass('flipped')
    }); // this is not work
});

感谢您的回答。

2 个答案:

答案 0 :(得分:2)

由于.flipped本质上是动态创建的元素,因此您需要使用事件委派:

http://jsfiddle.net/isherwood/7Zg92/

$(document).on('click', '.close', function () {
    $('.flip').find('.card.flipped').removeClass('flipped')
});

答案 1 :(得分:2)

因为事件冒泡,你应该停止事件的传播:

$('.close').click(function(event) {
    event.stopPropagation();
    $('.flip').find('.card.flipped').removeClass('flipped');
});

http://jsfiddle.net/aaLUs/