jQuery,检查.detach变量是否为空或存储了分离的数据?

时间:2012-11-05 11:52:09

标签: jquery

我有一个下面的函数,当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 ) {

但我不认为它的工作正常。有什么想法吗?

3 个答案:

答案 0 :(得分:2)

使用jQuery对象的length属性:

if (detachedMini.length) {
    $('#bike-mini-slider .bike').append(detachedMini);
    $('#bike-main slider .bike').append(detachedMain);
}

答案 1 :(得分:0)

尝试

if ( detachedMini.length != 0 )

答案 2 :(得分: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();

};