最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery empty selector (null selector) - Stack Overflow

programmeradmin1浏览0评论

Sometimes it is useful to have an empty jQuery object, to be used either as a default value or as an initial value, when constructing a collection of items.

For an example, see here.

One way to do it would be to use a selector which is unlikely to match anything, like $('skdhjfksjdhfksjhdf'), but this is obviously inelegant.

How can I get an empty jQuery object in elegant style ?

Sometimes it is useful to have an empty jQuery object, to be used either as a default value or as an initial value, when constructing a collection of items.

For an example, see here.

One way to do it would be to use a selector which is unlikely to match anything, like $('skdhjfksjdhfksjhdf'), but this is obviously inelegant.

How can I get an empty jQuery object in elegant style ?

Share Improve this question edited Jul 14, 2010 at 15:26 itsadok 29.3k31 gold badges132 silver badges174 bronze badges asked Aug 4, 2009 at 16:17 Peter KaletaPeter Kaleta 3853 silver badges9 bronze badges 5
  • Where would you need an empty selector? – Randell Commented Aug 4, 2009 at 16:18
  • Interesting, what's the point? – Ryan Florence Commented Aug 4, 2009 at 16:19
  • What do you mean? Perhaps further explanation and complete sentences would help garner an answer. – Michael Brown Commented Aug 4, 2009 at 16:42
  • Really not bad enough to warrant two down votes. – Oskar Austegard Commented Oct 14, 2010 at 3:13
  • I'm using empty jQuery object when I need to make an operation on an undetermined number of nodes (where zero is an acceptable solution). It can be to add a class on several inputs when validating a form or to filter a list of items. – Shaffe Commented Sep 4, 2016 at 9:44
Add a comment  | 

2 Answers 2

Reset to default 34

Starting with jQuery 1.4, a simple $() will return an empty set. jQuery 1.4 release notes ("jQuery() returns an empty set").

For earlier versions, use $([])

Do you mean...

//just get jQuery...
var foo = $();

//or just get the browser using jQuery...
if($.browser.msie){
  alert('You are using the blue e!');
}
发布评论

评论列表(0)

  1. 暂无评论