0

問題は、検索ページが開いているときにすべてのリンクがアクティブになっていないことです。だから、私は問題がこのJQUERYコードから来たことを発見しました:

var shouldShow = $.cookie('show_desc') == 'yep';
            if( shouldShow ) { $('#searchunder').show(); $('body').animate({ paddingTop: 80 }); }
            else {             $('#searchunder').hide(); $('body').animate({ paddingTop: 25 }); }

        // shows the group_desciption on clicking the noted link
        $('#search').click(function() {
            $('body').animate({ paddingTop: 80 });
            $('#searchunder').show('fast');
            $.cookie('show_desc', 'yep');
            return false;
        });
        // hides the group_desciption on clicking the noted link
        $('#close').click(function() {
            $('body').animate({ paddingTop: 25 });
            $('#searchunder').hide('fast');
            $.cookie('show_desc', 'nope');
            return false;
        });

//return false;を追加すると、リンクは機能しています。しかし、私が返すとfalseを削除します。検索バーが非表示になり、再び開きます。では、解決策は何でしょう?

4

1 に答える 1

1

jQuery ハンドラーでreturn falseは、次の 2 つのことを行います。 伝達を停止し、既定のアクションを防止します。

あなたが示したコードがリンクをたどるのを防ぐことができる唯一の方法は、リンクがand/or要素にある場合です。それが問題である場合は、次のように解決できます。#search#close

$('#search').click(function() {
    if (!$(e.target).closest('a')[0]) { // Check if click was on `a` element
        $('body').animate({ paddingTop: 80 });
        $('#searchunder').show('fast');
        $.cookie('show_desc', 'yep');
        return false;
    }
});

(同様に#close.)

...クリックがa要素上になかった場合にのみ、関連するコードを実行します。

注: 上記では、 and はそれ自体が要素ではなく、要素内にないことを前提と#search#closeaます a。たとえば、リンクは含まれていますが、リンクではなく、リンクに含まていません。それも解決できますが、必要なのはほんの少しのコードだけです。

if (!$(e.target).closest('a')[0]) { // Check if click was on `a` element

それはそのようになります

if (!$(e.target).parentsUntil(this).filter('a')[0]) { // Check if click went through an `a` element en route to us
于 2013-10-03T11:55:21.823 に答える