为什么我一直调用不了这段js代码? 新人真心求指导,谢谢html文档:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Image Gallery</title>
<script type="scripts/showPic.js"></script>
</head>
<body>
<h1>Snapshots</h1>
<ul id="imagegallery">
<li>
<a href="images/1.jpg"   title="1">1</a>
</li>
<li>
<a href="images/2.jpg"  title="2">2</a>
</li>
<li>
<a href="images/3.jpg"  title="3">3</a>
</li>
<li>
<a href="images/4.jpg"  title="4">4</a>
</li>
</ul>
<img src="images/5.jpg" alt="1234" name="placeholder" id="placeholder" />
<p id="description">1234。</p>
</body>
</html>javascript代码:
function showPic(whichpic){
if(document.getElementById("placeholder")) return false;
var source=whichpic.getAttribute("href");
var placeholder=document.getElementById("placeholder");
if (placeholder.nodename!="IMG") return false;
placeholder.setAttribute("src",source);
if(document.getElementById("description")){
var text=whichpic.getAttribute("title")?whichpic.getAttribute("title"):"";
var description=document.getElementById("description");
if(description.firstChild.nodeType==3){
description.firstChild.nodeValue=text;
 }
}
return false;
}function prepareGallery(){
if(!document.getElementsByTagName) return false;
if(!document.getElementById) return false;
if(!document.getElementById("imagegallery")) return false;
var gallery=document.getElementById("imagegallery");
var links=gallery.getElementsByTagName("a");
for(var i=0; i<links.length;i++){
links[i].onclick=function(){
return showPic(this)? false: true;

}
}
}function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}addLoadEvent(prepareGallery);

解决方案 »

  1.   

    window.onload事件中调用prepareGallery()
    在prepareGallery()中调用showPic()但你偏偏showPic()中开头第一句就是
    if(document.getElementById("placeholder")) return false;
    这个<img src="images/5.jpg" alt="1234" name="placeholder" id="placeholder" />
    不是已经存在了么?那你一执行到这里就已经return false了
    你是不是想写
    if(!document.getElementById("placeholder")) return false;呀?还有些写法上也有问题。比如:
    placeholder.nodename应该是:placeholder.nodeName自己调试的时候,要是感觉代码没有被执行。最简单的测试方法就是在某个你认为应该执行的地方加一个alert('1')。看看是否已经被执行
      

  2.   

    <script type="scripts/showPic.js"></script>
    你的引入方式
    <script language="JavaScript" src="/js/jquery-1.8.2.min.js" type="text/javascript"></script>