最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Equivalent of "parentNode" in internet explorer - Stack Overflow

programmeradmin1浏览0评论

I wrote some code that modifies the images on a webpage. Works with firefox and safari. But tryingto get it to work with Internet explorer has got me stumped. What is the equivalent of "parentNode" in explorer? Or how does one trick it into working?

images = document.getElementsByTagName('img')
parms = {};

for (a=0;a < images.length;a++){
    parent = images[a].parentNode; // <-- What to substitute for explorer?
    parms[a] = {};
    parms[a].bigsrc=parent.getAttribute("href");
    parms[a].w_o = images[a].width;
    parms[a].h_o = images[a].height;
    parms[a].IsBig = false;
    parms[a].loaded = false;
    images[a].border=0;
    parent.setAttribute("href","javascript:MakeBig('"+a+"')");
}

I wrote some code that modifies the images on a webpage. Works with firefox and safari. But tryingto get it to work with Internet explorer has got me stumped. What is the equivalent of "parentNode" in explorer? Or how does one trick it into working?

images = document.getElementsByTagName('img')
parms = {};

for (a=0;a < images.length;a++){
    parent = images[a].parentNode; // <-- What to substitute for explorer?
    parms[a] = {};
    parms[a].bigsrc=parent.getAttribute("href");
    parms[a].w_o = images[a].width;
    parms[a].h_o = images[a].height;
    parms[a].IsBig = false;
    parms[a].loaded = false;
    images[a].border=0;
    parent.setAttribute("href","javascript:MakeBig('"+a+"')");
}
Share Improve this question edited Jun 26, 2010 at 22:39 Matthew Flaschen 285k53 gold badges521 silver badges552 bronze badges asked Jun 26, 2010 at 22:37 Matthias WandelMatthias Wandel 6,48310 gold badges35 silver badges31 bronze badges 3
  • 2 Just some general comments: use var to declare images, parms, a and parent locally; don't use setAttribute to set href, use parent.href instead (IE freaks out); you probably want parms to be an array (instead of a general object): var parms = [];; you probably want to set images[a].style.border = "none" instead of the border attribute, which is obsolete. – Marcel Korpel Commented Jun 26, 2010 at 22:57
  • Could you provide some HTML so we can reproduce the problem? – Tim Down Commented Jun 29, 2010 at 9:20
  • An example of the (works in firefox but not IE) code is here: woodgears.ca/workshop/jacques/min.html It's a cool image zooming effect, if only I could make it work with explorer (or opera, for that matter) – Matthias Wandel Commented Jun 29, 2010 at 18:12
Add a comment  | 

3 Answers 3

Reset to default 14

The problem is with the assignment of the parentNode to a var called "parent." This seems to be a reserved word in IE that breaks the code. Change the var name and it should work.

parentNode works fine in IE (except in certain cases, very likely irrelevant here). The error is almost certainly elsewhere in your code.

Are you expecting the parentNode to be an anchor? It looks like you're trying to just wrap the image in a link. If that's correct, what might work as an alternative is adding an onclick to the image itself, and setting a hand cursor. That could create the appearance of the image being a link without you having to care what the parentNode is.

发布评论

评论列表(0)

  1. 暂无评论