<!DOCTYPE html > <html lang="en">
<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <title>Image Gallery</title>
  <script type="text/javascript" src="scripts/showPic.js"></script>
 <!-- <link rel="stylesheet" href="styles/layout.css" type="text/css" media="screen" />-->
</head>
<body>
  <h1>Snapshots</h1>
  <ul id="imagegrally">
    <li>
      <a href="images/fireworks.jpg" title="A fireworks display" class="itcast">Fireworks</a>
    </li>
    <li>
      <a href="images/coffee.jpg" title="A cup of black coffee" class="itcast">Coffee</a>
    </li>
    <li>
      <a href="images/rose.jpg" title="A red, red rose" class="itcast">Rose</a>
    </li>
    <li>
      <a href="images/bigben.jpg" title="The famous clock" onclick="showPic(this); return false;">Big Ben</a>
    </li>
  </ul>
  <img id="placeholder" src="images/placeholder.gif" alt="my image gallery" />
  <p id="description">Choose an image.</p>
</body>
</html>
window.onload=prepareGrally();function prepareGrally(){
if(!document.getElementById)  return false;
if(!document.getElementsByTagName) return false;
if(!document.getElementById("imagegrally")) return false;
var links = document.getElementsByTagName("a");
for(var i=0;i<links.length;i++){
if(links[i].getAttribute("class")=="itcast"){
links[i].onclick=function(){
showPic(this);
return false;
}
}
}
}function showPic(whichpic) {
 var source = whichpic.getAttribute("href");
  var placeholder = document.getElementById("placeholder");
  placeholder.setAttribute("src",source);
  var text = whichpic.getAttribute("title");
  var description = document.getElementById("description");
  description.firstChild.nodeValue = text;
}为什么没用啊....快TMD的疯了...

解决方案 »

  1.   

    script 标签放</body>前面
      

  2.   

    window.onload=prepareGrally();
    改为
    window.onload=prepareGrally;
      

  3.   


    prepareGrally();  后面的() 跟在在函数对象  表示一个 函数运行 表达式 这个时候 prepareGrally 被运行了window.onload=prepareGrally();  表示  window.onload=prepareGrally函数运行后的结果
    window.onload=prepareGrally  表示 window.onload=prepareGrally 这个函数本身为什么要这样写  是因为你的 整体逻辑  表现 你希望  window。onload事件时执行prepareGrally函数
      

  4.   


    prepareGrally();  后面的() 跟在在函数对象  表示一个 函数运行 表达式 这个时候 prepareGrally 被运行了window.onload=prepareGrally();  表示  window.onload=prepareGrally函数运行后的结果
    window.onload=prepareGrally  表示 window.onload=prepareGrally 这个函数本身为什么要这样写  是因为你的 整体逻辑  表现 你希望  window。onload事件时执行prepareGrally函数
    谢了