我开始学习 javascript,我一辈子都想不通,为什么控制台会抛出这个“无法读取属性 'parentNode'”;以及为什么 for 循环在第三个链接之后调用 an。
我正在学习有关 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>
谢谢