简体   繁体   中英

external link not working javascript

I have some basic javascript that was originally in the html file but then I have put it in an external file and it is not working, I am not sure why. I have put the script just before the closing body tag.

Here is my code: http://jsfiddle.net/2fuxR/

HTML

   <body>

  <div class="titleBar"><img src="../assets/images/gallery.png" alt=""></div>
  <div id="display">
  <img id="displayimg">
  </div>
  <div id="select">
  <img id="selectimg" src="../assets/images/gallery/gallery1.png"        onclick='javascript:display(this)'>

    <img id="selectimg" src="../assets/images/gallery/gallery2.png" onclick='javascript:display(this)'>
    <img id="selectimg" src="../assets/images/gallery/gallery3.png" onclick='javascript:display(this)'>
    <img id="selectimg" src="../assets/images/gallery/gallery4.png" onclick='javascript:display(this)'>
    <img id="selectimg" src="../assets/images/gallery/gallery5.png" onclick='javascript:display(this)'>
    <img id="selectimg" src="../assets/images/gallery/gallery6.png" onclick='javascript:display(this)'>

  </div>
  <script src="script.js"></script>
  </body>

JAVASCRIPT

 function display(imgElement){
   document.getElementById('displayimg').setAttribute('src',imgElement.getAttribute('src'));

您忘记了jscript函数末尾的} ,并尝试在您的js src中添加斜杠<script src="/script.js"></script>

i have test is in my native environment ,it works well!
you just need to pay attention to the path of the external js file
and check your js function's syntax ,firebug and chrome developer tool may be a good helper

Your jsFiddle framework & extensions set the no - library and onLoad

chaged jsFiddle framework & extensions set no - library and no wrap in .

edit your html code.

try it

<div id="display">   
    <img id="selectimg" src="../assets/images/gallery/gallery2.png" onclick='display(this)'/>
    <img id="selectimg" src="../assets/images/gallery/gallery3.png" onclick='display(this)'/>
    <img id="selectimg" src="../assets/images/gallery/gallery4.png" onclick='display(this)'/>
    <img id="selectimg" src="../assets/images/gallery/gallery5.png" onclick='display(this)'/>
    <img id="selectimg" src="../assets/images/gallery/gallery6.png" onclick='display(this)'/>
</div>

jsFiddle : http://jsfiddle.net/WonSeokHan/2L9ar/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM