admin管理员组文章数量:1417406
I'cant get parentNode.tagName from this code and put some style to li
tag.
// path to catalog images
var img = $('.catalog ul li > img');
for (i = 0; i < img.length; i++) {
var pic_real_width;
// Make in memory copy of image to avoid css issues
$("<img/>").attr("src", $(img[i]).attr("src")).load(function () {
pic_real_width = this.width;
if (pic_real_width > 500) {
// this part of code not work
var imageParent = this.parenNode.tagName = 'li';
imageParent.style.width = '50%';
}
});
}
I'cant get parentNode.tagName from this code and put some style to li
tag.
// path to catalog images
var img = $('.catalog ul li > img');
for (i = 0; i < img.length; i++) {
var pic_real_width;
// Make in memory copy of image to avoid css issues
$("<img/>").attr("src", $(img[i]).attr("src")).load(function () {
pic_real_width = this.width;
if (pic_real_width > 500) {
// this part of code not work
var imageParent = this.parenNode.tagName = 'li';
imageParent.style.width = '50%';
}
});
}
Share
Improve this question
edited May 21, 2013 at 13:32
user1106925
asked May 21, 2013 at 13:19
Constantine GolubConstantine Golub
3517 silver badges22 bronze badges
2
-
But where in your code are you trying to read
parentNode.tagName
?? – techfoobar Commented May 21, 2013 at 13:21 - How are we supposed to help you without the associated HTML ? I would suggest making a fiddle... – Laurent S. Commented May 21, 2013 at 13:21
1 Answer
Reset to default 3You have parenNode
instead of parentNode
, and you seem to be assigning to that property, which is read-only.
// ---------------------v v---assignment won't work
var imageParent = this.parentNode.tagName = 'li';
If you just wanted the parent, there's no reason to mess with the .tagName
.
var imageParent = this.parentNode;
imageParent.style.width = '50%';
Also, I don't see where you're appending the new <img>
to the DOM. If it's not appended, it won't have a parent.
Maybe you meant to get the parent of the original image. To do that, each load()
handler will need to close over the current i
or img
. You can use .each()
to acplish this.
$('.catalog ul li > img')
.each(function(i, img) {
var pic_real_width;
$("<img/>").attr("src", $(img).attr("src")).load(function () {
pic_real_width = this.width;
if (pic_real_width > 500) {
var imageParent = img.parentNode;
imageParent.style.width = '50%';
}
});
})
本文标签: javascriptGet parentNode tagNameStack Overflow
版权声明:本文标题:javascript - Get parentNode tagName - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745262929a2650437.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论