Я начинаю изучать javascript, и я не могу понять, что за жизнь меня, почему этот «Невозможно прочитать свойство« parentNode »получает бросок в консоли; и почему цикл for вызывает по третьей ссылке.Невозможно прочитать свойство «parentNode»
Я следовал уроку по опрокидыванию javascript, работает опрокидывание, но почему возникает ошибка?
<style type="text/css" media="screen">
#caption, #content {
width: 30%;
float: left;
padding: 10%;
line-height: 1.675em;
}
</style>
<script type="text/javascript" charset="utf-8">
window.onload = initRollover;
function initRollover() {
for (var i = 0; i < document.links.length; i++) {
console.log(i);
if(document.images[i].parentNode.tagName == "A") {
setupRollover(document.images[i]);
}
document.links[i].setAttribute('class', 'test');
}
}
function setupRollover(thisImage){
thisImage.outImage = new Image();
thisImage.outImage.src = thisImage.src;
thisImage.onmouseout = function() {
this.src = this.outImage.src;
}
thisImage.overImage = new Image();
thisImage.overImage.src = "images/" + "silver" + ".jpg";
thisImage.onmouseover = function(){
this.src = this.overImage.src;
}
}
</script>
<div id="content" style="background: lightgreen;">
<img src="images/silver.jpg" width="240" alt="Silver" id="photoA">
<br><br>
<a href="boom.html">
<img src="images/gold.jpg" width="240" alt="Gold" id="photoB">
</a>
</div>
<div id="caption">
<a href="foo.html">Lorem ipsum dolor sit amet,</a> consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. <a href="bar.html">Duis aute irure dolor </a>in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in <a href="car.html">culpa qui officia deserunt</a> mollit anim id est laborum.
</div>
<div style="clear:both"></div>
Благодаря
Вы перебираете 'document.links', но затем используете' document.images [i] 'в цикле. Поэтому, если у вас больше ссылок, чем у изображений, вы получите сообщение об ошибке. – nnnnnn