开发者

when traversing the DOM in jquery, how can I figure out which node I am on?

If I am traversing the DOM using jquery, is there 开发者_如何学Ca way to output the type of tag I am on?

the tags' dont have ID's so I can't do:

.attr("id")

I am thinking of just alerting this value so I know roughly which tag I am on.


You use tagName to get the tag:

$('.class').get(0).tagName;

Or

$('.class')[0].tagName;

Bear in mind this comes from the DOM element, not from the jQuery selector.


Are you developing in Firefox? Use Firebug and FireQuery. Then if you want to examine an element, just stick a console.log($(curiousElement)); in your code. Firebug will give you a nice summary. With FireQuery, though, you can examine the entire jQuery object.


Wouldn't that be the .context to get the dom object that was passed to jQuery?

See... http://api.jquery.com/context/


Also you could highlight the current element, e.g. by adding a CSS class that overrides its background color or border…


If you're traversing the DOM using next() and previous() you should typically know what kind of element you anticipate coming next. If you're not sure, you can also supply selectors to both of these methods to skip to what you know you are looking for:

$('currentElement').next('div')

You can use the tagName attribute from the DOM Element like the other answer, but you can also do something like this if you know what you're expecting and are verifying nothing has changed:

var $next = $('currentElement').next();

if ($next.is('a')) { ... }

So you can check for whether the element you found is the element you expected to be there, and is() takes selectors as well so you can test for ID's or classes.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜