Test for empty jQuery selection result

前端 未结 3 1973
余生分开走
余生分开走 2021-01-31 13:08

Say I do

var s = $(\'#something\');

and next I want to test if jQuery found #something, i.e. I want to test if s is empty.

<
3条回答
  •  星月不相逢
    2021-01-31 13:43

    An even more jQueryesque solution for me is:

    jQuery.fn.isEmpty = function(fun){ if (this.length === 0) { fun(); } return this; };
    

    This lets me write in typical fashion:

    $("#sel").fadeOut(500,afterFade).isEmpty(insteadOfFade);
    

提交回复
热议问题