Test for empty jQuery selection result

前端 未结 3 2030
余生分开走
余生分开走 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:54

    Use the s.length property.

    if(s.length == 0) {
        ...
    }
    

    [edit] size() deprecated in jquery 1.8 http://api.jquery.com/size/

提交回复
热议问题