开发者

How to get the tagName of next selector?

开发者 https://www.devze.com 2023-01-13 04:46 出处:网络
I need to verify is the next element 开发者_运维问答of each <a> tag <img> or not? so, i need to get the tagName of each <a> element\'s next tag.

I need to verify is the next element 开发者_运维问答of each <a> tag <img> or not? so, i need to get the tagName of each <a> element's next tag.

$("a").each(function()
{
     how to verify it here?
});

Thanks


jAndy's answer is correct and most efficient, but you can also use jQuery's is() method:

$('a').each(function() {
    if($(this).next().is('img')) { ... }
});


$("a").each(function(){
    if(this.nextSibling && this.nextSibling.tagName && this.nextSibling.tagName.toLowerCase() == 'img'){
        //so something when the next element is an image
    }
});

Edit: added check for element: if there is not nextSibling, it would fail. Text nodes doesn't have a tagName, so check for it too.


If you're using the .tagname property on the DOM object, beware that In XHTML (or any other XML format), the element will be returned in lower case (e.g. 'img') In HTML you will get it in uppercase (e.g. 'IMG').

See https://developer.mozilla.org/en/DOM/element.tagName

You'll need to know this for your comparison.

0

精彩评论

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