开发者

Check if given selector matches given object [duplicate]

This question already has answers here: Closed 9 years ago.

Possible Duplicate:

How can I tell whether an element matches a selector?

I've been using jQuery for this javascript framework I've been working on and I was wondering if there is a way of checki开发者_如何学编程ng if a given selector matches a given jQuery object. Something like:

var divObj = $(document.createElement('div'));
var result = divObj.hasSelector('div');

Result would either return a "true" or the object back if the selector matches the object.


You can tell if an object is a div using:

$('selector').is('div');

Or for a direct comparison:

$('selector1')[0].tagName == $('selector2')[0].tagName


There's a .is() function exactly for this:

var divObj = $(document.createElement('div')); 
var result = divObj.is('div');

You can test it out here. Note that this is a simple case, be aware in other situations .is() returns true if any of the elements in your jQuery object you're running it on matches the selector.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜