2011-03-02 241 views
1

此代碼在Firefox中加載時工作,但在IE8中不起作用。在IE8中,單擊鏈接不起作用。Javascript適用於Firefox,不適用於IE8?

我將不勝感激任何關於如何使其在IE中工作的指針。

 <html> 
     <head> 
     <title>RG</title> 
     </head> 
    <script type="text/javascript"> 

     var rightTarget; 
     var leftTarget; 
     var index = 516; 
     var img; 
     function rightLinkClicked(e) { 
     rightTarget = e.target; 
      rightTarget.style.color = "green"; 
     if (index < 547) { 
     index = index +1; 
       img=document.getElementById("img"); 
       img.src="pics/IMG_0" + index + ".JPG"; 
      } else { 
     alert ("This is the last painting"); 
     } 
     } 

     function leftLinkClicked(e) { 
     leftTarget = e.target; 
      leftTarget.style.color = "red"; 
      rightTarget.style.color = "black"; 
     if (index >516) { 
      index = index -1; 
        img=document.getElementById('img'); 
        img.src="pics/IMG_0" + index + ".JPG"; 
     } else { 
     alert ("This is the first painting"); 
      } 
      } 

     function addListeners() { 
     var rightLink = document.getElementById("rightlinkid"); 
      rightLink.addEventListener('click', rightLinkClicked, false); 
      var leftLink = document.getElementById("leftlinkid"); 

      leftLink.addEventListener('click', leftLinkClicked, false); 
      } 
      window.addEventListener('load', addListeners, false); 

     </script> 
     </head> 
     <body> 
     <h2>RG</h2> 
     <div> 
     <a id="leftlinkid">Previous 
    <img src="icons/left.gif" ;="" alt="left arrow" title=""> 
     </a> 
     </div> 
    <div id="myimg">   
      <img id="img" src="pics/IMG_0516.JPG" ;="" alt="start arrow"   title=""width="640"height="480"> 
</div> 
<div> 
<a id="rightlinkid">Next 
<img src="icons/right.gif" ;="" alt="right arrow" title=""> 
</a> 
</div>   
<p>© 2011 RG</p> 
    </body> 
    </html> 

回答

1

IE使用attachEvent代替addEventListener

您可能希望使用跨瀏覽器的Javascript庫(例如jQuery)來處理這些問題(以及更多)。

+0

謝謝,關於如何修改代碼以使用jQuery的任何快速指針? – Tori 2011-03-02 21:36:17

+0

http://api.jquery.com/click – SLaks 2011-03-02 21:47:34

相關問題