0

我在下面有一个 this 函数,当 onClick 发生 + 使用 ref 时被触发。

我已经设法让 .detach 成功工作。

但是当我的函数第二次运行时,我想检查它是否detachedMini真的包含任何东西,以便我可以重新附加它,然后再分离下一个。

bikeFilter = function (y) {

    var detachedMini,
        detachedMain;

    bikeDetach = function () {

        detachedMini = $('#bike-mini-slider .bike[data-group!=' + y + ']');
        detachedMain = $('#bike-main-slider .bike[data-group!=' + y + ']');

        detachedMini.detach();
        detachedMain.detach();

    };

    if ( !detachedMini == 0 ) {

        $('#bike-mini-slider .bike').append(detachedMini);
        $('#bike-main slider .bike').append(detachedMain);

    }                       

    bikeDetach();

};

我试过这个...

if ( !detachedMini == 0 ) {

但我不认为它工作正常。有任何想法吗?

4

3 回答 3

2

使用 jQuery 对象的长度属性:

if (detachedMini.length) {
    $('#bike-mini-slider .bike').append(detachedMini);
    $('#bike-main slider .bike').append(detachedMain);
}
于 2012-11-05T11:57:03.897 回答
0

尝试

if ( detachedMini.length != 0 )
于 2012-11-05T11:57:52.520 回答
0

怎么样:

bikeFilter = function (y) {

    var detachedMini = null,
        detachedMain = null;

    bikeDetach = function () {

        detachedMini = $('#bike-mini-slider .bike[data-group!=' + y + ']').detach();
        detachedMain = $('#bike-main-slider .bike[data-group!=' + y + ']').detach();

    };

    if ( typeof(detachedMini) != "null" ) {

        $('#bike-mini-slider .bike').append(detachedMini);
        $('#bike-main slider .bike').append(detachedMain);

    }                       

    bikeDetach();

};
于 2012-11-05T11:58:26.180 回答