0

フォームが送信された後に成功メッセージを表示するために jQuery を使用しています。フォームは wordpress プラグイン Contact Form 7 を使用して作成されます。クラスwpcf7-mail-sent-okはプラグイン ajax 送信スクリプトによって動的に追加されます。ユーザーがメッセージをクリックすると、フェードアウトして消えるようにしようとしています。何らかの理由で removeClass メソッドが機能していません。

それが機能しない理由を誰かが見ることができますか? 「alert()」呼び出しでテストしたところ、タイムアウト機能は確実に機能しています。ご協力いただきありがとうございます。

PS ...私はLESS cssを使用しているため、ここに投稿されたcssの.opacity()構文について説明しています。

HTML:

<div class="wpcf7-response-output wpcf7-mail-sent-ok"><div class="image"></div></div>

Jquery + CSS

        var $sent = $('.wpcf7-mail-sent-ok ');

        function remove() {$sent.hide().removeClass('wpcf7-mail-sent-ok hide').removeAttr('style')}

        $sent.live("click", function(){
            $(this).addClass('hide');
            setTimeout(remove,400)
        });

.wpcf7-response-output {
    width: 100%;
    height: 100%;
    position: fixed;
    top: 0;
    left: 0;
    z-index: -1;
    background: transparent;
    opacity: 0;
    -moz-opacity: 0;
    .transition(opacity,.4s);
}

.wpcf7-response-output.wpcf7-mail-sent-ok .image {
    width: 400px;
    height: 138px;
    display: block;
    position: absolute;
    z-index: 2000; 
    top: 50%; 
    left: 50%; 
    margin: 0;
    background: url(assets/images/loading.png) no-repeat;
    background-size: 0 0;

    -webkit-transition: margin .4s ease-in-out;
    -moz-transition: margin .4s ease-in-out;
    -o-transition: margin .4s ease-in-out;
    -ms-transition: margin .4s ease-in-out;
    transition: margin .4s ease-in-out;

    -webkit-animation: pulse 400ms ease-out infinite alternate;
    -moz-animation: pulse 400ms ease-out infinite alternate;
    -o-animation: pulse 400ms ease-out infinite alternate;
    animation: pulse 400ms ease-out infinite alternate
}

.wpcf7-response-output.wpcf7-mail-sent-ok {z-index: 1000; background-color: rgba(255,255,255,.7); .opacity(1)}

.wpcf7-response-output.wpcf7-mail-sent-ok .image {
    height: 132px;
    position: absolute;
    margin: -66px 0 0 -200px;
    background-size: 100% 100%;
    background: url(assets/images/img-sent.png) no-repeat center center;
}

.wpcf7-mail-sent-ok.hide {.opacity(0); z-index: -1}
4

2 に答える 2

1

function を定義した時点でremove、 の値は、$sentどの要素とも一致しない jQuery オブジェクトであると既に判断されているため、機能しません。これは、書き込みを行うとすぐにマッチングが行われるためです。

var $sent = $('.wpcf7-mail-sent-ok '); 

現時点では、「メール送信済み」要素はまだありません。

これを修正する最も簡単な方法は、 内でセレクターを再評価することですremove

function remove() {
    $('.wpcf7-mail-sent-ok').hide()
                            .removeClass('wpcf7-mail-sent-ok hide')
                            .removeAttr('style');
} 

別の解決策はthis、クリック ハンドラー内で使用し、パラメーターとして に渡すことremoveです。

function remove(el) {
    $(el).hide()
         .removeClass('wpcf7-mail-sent-ok hide')
         .removeAttr('style');
} 

$sent.live("click", function(){ 
    $(this).addClass('hide'); 
    setTimeout(function() { remove(this); },400) 
}); 

もちろん、jQuery のビルトインを使用して、完全delayに取り除く方がさらに良いでしょう:remove

$sent.live("click", function(){ 
    $(this).addClass('hide')
           .delay(400)
           .hide(0) // need to pass 0 as a parameter
           .removeClass('wpcf7-mail-sent-ok hide')
           .removeAttr('style');
}); 
于 2012-01-21T23:34:30.253 に答える
1

要素をフェードアウトするコードが表示されません。機能しない理由は、@Jon が述べたものと同じです。匿名関数の使用を試みることができます。この関数の内部では、トリガーthisされる要素がポイントされます。clickこれを試して。

    $('.wpcf7-mail-sent-ok ').live("click", function(){
        var $this = $(this).addClass('hide');
        setTimeout(function(){
            $this
            .hide()
            .removeClass('wpcf7-mail-sent-ok hide')
            .removeAttr('style')
        },400)
    });
于 2012-01-21T23:44:22.623 に答える