Determining object type of a Jquery selector
I have a listbox which (sometimes) uses OptionGroups as well, meaning that if I select the parent of开发者_JS百科 a certain OPTION in my listbox, as follows:
var optionGroup = $(this.options(index)).parent();
it can either be an OptionGroup (if they're used), or the Select element itself.
Is there an easy way of determining which kind of object I'm dealing with here?
You could use .parent('optgroup')
. This will filter out anything that isn't an optiongroup. Then you'd have to check if you get a result of course.
The information you are looking for is the tagName
property of the DOM element. You'll get hold of this from a jQuery element like so:
$("#element")[0].tagName
before comparing it, It's advisable to do a .toLowerCase()
on the property.
I think it could work with
if($(this.options(index)).parent().is('select'))
{
//your code here
}
Just throwing this out there, if you're actually wanting this to determine if you need another .parent()
to get the <select>
, there's a method for this, .closest()
, like this:
$(this.options(index)).closest('select');
精彩评论