admin管理员组文章数量:1410712
how i can check in this code that the parent in loop is span or div.
$('a img, span img').each(function(n){
var c= $(this).parent();
console.warn(c)
});
i want to know if this possible to check that find the parent tag is span or anchor in html
how i can check in this code that the parent in loop is span or div.
$('a img, span img').each(function(n){
var c= $(this).parent();
console.warn(c)
});
i want to know if this possible to check that find the parent tag is span or anchor in html
how i can check in this code that the parent in loop is span or div.
$('a img, span img').each(function(n){
var c= $(this).parent();
console.warn(c)
});
i want to know if this possible to check that find the parent tag is span or anchor in html
how i can check in this code that the parent in loop is span or div.
$('a img, span img').each(function(n){
var c= $(this).parent();
console.warn(c)
});
i want to know if this possible to check that find the parent tag is span or anchor in html
Share Improve this question asked Jan 12, 2012 at 7:52 Anirudha GuptaAnirudha Gupta 9,3199 gold badges58 silver badges83 bronze badges4 Answers
Reset to default 6You can use is
:
$('a img, span img').each(function(n){
var c = $(this).parent();
if(c.is("span, a")) {
//It's a span or an anchor!
}
});
The is
method returns true
if any of the selected elements match the selector. In this case, the selected element is the parent, and the selector looks for either a span
or an a
element.
Alternatively, you could use parent
with a selector and check the length of the resulting object:
$('a img, span img').each(function(n){
if($(this).parent("span, a").length) {
//It's a span or an anchor!
}
});
The is
method is marginally faster though (don't be misled by the apparently huge difference... the numbers are both quite low):
$(this).parent().is(/* selectors */);
Well you can get the tagname, as:
$('a img, span img').each(function(n){
a = $(this).parent();
console.log(a[0].nodeName)
});
Hope it helps
try
$(this).parents('div')<br/>
$(this).parents('span')<br/>
find parent div or span element
本文标签: javascripthow i can check that parent is span or anchor in htmlStack Overflow
版权声明:本文标题:javascript - how i can check that parent is span or anchor in html? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744274886a2598362.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论