javascript - Get parentNode tagName -
i'cant parentnode.tagname code , put style li
tag.
// path catalog images var img = $('.catalog ul li > img'); (i = 0; < img.length; i++) { var pic_real_width; // make in memory copy of image avoid css issues $("<img/>").attr("src", $(img[i]).attr("src")).load(function () { pic_real_width = this.width; if (pic_real_width > 500) { // part of code not work var imageparent = this.parennode.tagname = 'li'; imageparent.style.width = '50%'; } }); }
you have parennode
instead of parentnode
, , seem assigning property, read-only.
// ---------------------v v---assignment won't work var imageparent = this.parentnode.tagname = 'li';
if wanted parent, there's no reason mess .tagname
.
var imageparent = this.parentnode; imageparent.style.width = '50%';
also, don't see you're appending new <img>
dom. if it's not appended, won't have parent.
maybe meant parent of original image. that, each load()
handler need close on current i
or img
. can use .each()
accomplish 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%'; } }); })
Comments
Post a Comment