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

javascript - How can I check my object type is Image? - Stack Overflow

programmeradmin3浏览0评论

If I write something like this:

var img = $(new Image()).attr('src', image.src);

How can I check later if img var is an image and not something else ?

If I write something like this:

var img = $(new Image()).attr('src', image.src);

How can I check later if img var is an image and not something else ?

Share Improve this question asked Sep 2, 2009 at 12:31 vsyncvsync 131k59 gold badges340 silver badges423 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6
 if ( img.is('img') ){

 }

for safety I may be tempted to wrap the var in jQuery again just incase you may have changed the img to a dom node or something else...

if ( $(img).is('img') ){

}
img.filter('img')

If this returns something then it is an image.

You should avoid explicit type checking.

Use polymorphism to select what has to be done with images, and what has to be done with other objects.

var img = $(new Image())(...);
img.process = function(){ ... do whatever images need ... };
objs.push( img );

var txt = new Text();
txt.process = function(){ .. do text processing, spellcheck, ... };
objs.push( txt );

...

objs.each( o ) { o.process(); }
发布评论

评论列表(0)

  1. 暂无评论