How can I detect if a selector returns null?

前端 未结 8 1949
无人及你
无人及你 2020-11-27 09:01

What is the best way to detect if a jQuery-selector returns an empty object. If you do:

alert($(\'#notAnElement\'));

you get [object Object

8条回答
  •  悲哀的现实
    2020-11-27 09:37

    This is in the JQuery documentation:

    http://learn.jquery.com/using-jquery-core/faq/how-do-i-test-whether-an-element-exists/

      alert( $( "#notAnElement" ).length ? 'Not null' : 'Null' );
    

提交回复
热议问题