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.
精彩评论