1

セットアップであるプロパティ リスト (マイ ショートリスト) のさまざまな種類の動作を処理する関数があり<ul><li>ます。<li>動作の 1 つのタイプは、各アイテム内のボタンをクリックしたときにプロパティ リスト アイテムを削除することですif。これは正常に機能していますが、すべてのアイテムが削除されたことを確認する私のステートメントは機能していません。

私が間違っていることを教えてもらえますか?onclickボタンのイベントと危険なifステートメントを介してアイテムの削除を処理する関数の一部を次に示します。

// Remove an item from the shortlist
$(this).find('.btn-minus').click(function() {
    var btnRemove = $(this);
    var propTile = $(this).parents('.property-tile');
    var propList = $('#property-listings');
    // If IE 8 / 7
    if ($('html').hasClass('lte8')) {
        propTile.hide('slide', 300, function()  {
            btnRemove.data("tooltip").hide();
        });
    }
    // All other browsers
    else {
        propTile.fadeOut(200, function()  {
            btnRemove.data("tooltip").hide();
            $(this).remove();
        }); 
    }
    if (propTile.length === 0) {
        propList.remove();
    }
});

関数の呼び出しは次のとおりです。 $(".my-shortlist").myShortlist();where.my-shortlist<ul>要素です。

ありがとう

4

2 に答える 2

0

私はそれを機能させました、私はメソッド<li>を使用して最後を検出する必要があり、最後の削除で効果が終了したら。完全なコードは次のとおりです。nextAll()fadeOut<li>propList

// Remove an item from the shortlist
// http://stackoverflow.com/questions/2358205/invoking-a-jquery-function-after-each-has-completed
$(this).find('.btn-minus').click(function() {
    // Set variables
    var btnRemove = $(this);
    var propTile = $(this).parents('.property-tile');
    var propList = $(this).parents('#property-listings');
    var propItems = $(this).parents('.property-tile').nextAll(), count = propItems.length;
    // Remove entire list and other elements when all items have been removed
    function complete() {
        propList.remove();
        $('.sticky-panel-toolbar, .property-disclaimer, .sticky-panel').remove();
    } 
    // Remove individual items
    // If IE 8 / 7
    if ($('html').hasClass('lte8')) {
        propTile.hide('slide', 300, function()  {
            btnRemove.data("tooltip").hide();
            // Evoke complete() function
            if (count == 0) {
                complete();
            }
        });
    }
    // All other browsers
    else {
        propTile.fadeOut(400, function() {
            btnRemove.data("tooltip").hide();
            $(this).remove();
            // Evoke complete() function
            if (count == 0) {
                complete();
            }
        })
    }
});

乾杯

于 2012-06-06T00:21:33.940 に答える
0

「.property-tile」要素を変数に入れたので、後でそれらを非表示にしても、オブジェクト変数から削除されません。したがって、セレクターを再実行する必要があります。

if ($(this).parents('.property-tile').length == 0) {
    propList.remove();
}

また、Roger Lindsjö が述べたように、これらの要素が実際に非表示になる前にこのチェックを行うという問題があります。hide/fadeOut のコールバックでこれを行う必要があります。

于 2012-06-05T08:20:37.293 に答える