添加一个从HTML外部调用函数的onClick到JS?

时间:2018-01-31 17:54:31

标签: javascript jquery html

这是我的HTML:

<form name="imageForm">
  <table border=3>
  <tr>
    <td>
      <input onclick="displayImage();" type=button value="Display Random Image">
    </td>
  </tr>
  <tr>
    <td>
    <script type="text/javascript" src="trinit5.js"></script>
    </td>
  </tr>
  </table>
</form>

如何将此HTML代码链接到以下JavaScript文件?我想添加一个调用displayImage函数的onClick事件处理程序

var imagesArray = ["thumbs/lrgokublack.png", "thumbs/lrtrunks.png", "thumbs/lrgohan.png", "thumbs/lrmajinvegeta.png"];

function displayImage(){

    var num = Math.floor(Math.random() * 5);
    document.canvas.src = imagesArray[num];

}

1 个答案:

答案 0 :(得分:3)

&#13;
&#13;
var imagesArray = ["thumbs/lrgokublack.png", "thumbs/lrtrunks.png", "thumbs/lrgohan.png", "thumbs/lrmajinvegeta.png"];

function displayImage() {
  var num = Math.floor(Math.random() * imagesArray.length);
  window.canvas.src = imagesArray[num];
}
&#13;
<input onclick="displayImage()" type=button value="Display Random Image">
<img id="canvas"></img>
&#13;
&#13;
&#13;