The HTML loading order is loaded from top to bottom, if the script contains a reference JS script, you should put this script in the head tag, so that the page can be used to refer to the JS script content. If you want to set the Button.onclick () event in script, this script should be placed after the button declaration.
Example: index.jsp
<%@ Page Language="Java"Import="java.util.*"pageencoding="UTF-8"ContentType="text/html; Charset=utf-8"%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en"><HTML><Head><title>JavaScript and Button.onclick () events</title><Metahttp-equiv= "Pragma"content= "No-cache"><Metahttp-equiv= "Cache-control"content= "No-cache"><Metahttp-equiv= "Expires"content= "0"><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><!--full-Text related script scripts (including referenced script files) are placed in the head tag -<Scripttype= "Text/javascript"src= "Include.js"> functionfun1 () {alert ("The first script mode shows the time:" +Date ()); }</Script></Head><Body> <ButtonID= "Button11"onclick= "fun1 ()">The first script mode shows the time</Button> <ButtonID= "Button12"onclick= "fun2 ()">Show time by referencing JS file</Button> <ButtonID= "Button2">The second script mode shows the time</Button> <ButtonID= "Button3">The third script mode shows the time</Button> <Script>document.getElementById ("Button2"). onclick= function() {displaydate ()}; document.getElementById ("Button3"). onclick=DisplayDate2; functiondisplaydate () {document.getElementById ("Demo"). InnerHTML= "The second script mode shows the time:" +Date (); } functionDisplayDate2 () {document.getElementById ("Demo2"). InnerHTML= "The third script mode shows the time:" +Date (); } </Script> <BR>Time One:<PID= "Demo"></P>Time Two:<PID= "Demo2"></P></Body></HTML>
Include.js
function fun2 () { alert ("Show time by reference JS file:" + date ()); }
Enter the URL: http://localhost:8080/Test/index.jsp, as follows
JavaScript mates Button.onclick () Usage Summary