<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Document</title> <script type="text/javascript" src="scripts/JSscript.js"></script> </head> <body> <div id="prepareGallery"> <ul> <li><a href="images/zhou_1.jpg" title="zhou1">周杰伦1</a></li> <li><a href="images/zhou_2.jpg" title="zhou2">周杰伦2</a></li> <li><a href="images/zhou_3.jpg" onClick="showPic(this);return false;" title="zhou3">周杰伦3</a></li> </ul> <img src="images/eg.jpg" alt="my image gallery" id="placeholder" /> <p id="description">choose my image</p> </div> </body> </html>
window.onload=prepareGallery(); function prepareGallery(){ if(!document.getElementsByTagName) return false; if(!document.getElementById) return false; if(!document.getElementById('prepareGallery')) return false; var gallery=document.getElementById('prepareGallery'); var links=gallery.getElementsByTagName('a'); for(var i=0;i<links.length;i++){ links[i].onClick=function(){ return !showPic(this); } } } function showPic(whichpic){ if(!document.getElementById('placeholder')) return false; var source=whichpic.getAttribute('href'); var placeholder=document.getElementById('placeholder'); placeholder.setAttribute('src',source); if(document.getElementById('description')){ if(whichpic.getAttribute('title')){ var text=whichpic.getAttribute('title'); }else{ var text=""; } var description=document.getElementById('description'); description.firstChild.nodeValue=text; } return true; }
上面是HTML和JS的代码,在HTML代码中,第一、第二张图片无法实现功能,但是第三张图片标签中添加了(
onClick="showPic(this);return false;"
)代码才能实现这个功能,请教下上面的代码哪里出现了问题,新人求助,万分感谢!
qq_安逸_4
Forget_淡忘
_辣子
小布ever