<html> <body> <form> <input type="button" onClick="window.alert('Hi!');"> </form> </body> </html>當滑鼠點按按紐後, 會啟動 onClick 事件處理器, 此例是彈出一個訊息方塊。
例題:pr7_1.html
假如要處理的工作較多, 也可以使用函式, 如
<html> <head> <title>Events & Functions</title> <SCRIPT language="JavaScript"> <!-- function hi_and_bye() { window.alert('Hi!'); window.alert('Bye!'); } //--> </SCRIPT> </head> <body> <form> <input type="button" onClick="hi_and_bye();"> </form> </body> </html>
<html> <head> <title>Events & Functions</title> </head> <body> <form> <input type="button" value="Do not click here" onClick="window.alert('I told you not to click me!');"> </form> </body> </html>
<html> <body> <form> <a href="http://none" onClick="window.alert('Hey! You click me!'); return false;"> Don't Click Me!</a> </form> </body> </html>
<a href="http://www.pageresource.com" onMouseOver="window.alert('I told you not to click me!');"> Don't Try Clicking Me!</a>
例題:pr7_2.html
<a href="http://none" onMouseOut="window.alert('What, you didn\'t like my link?');"> Click Me!</a>
<body onLoad="window.alert('I\'m done loading now!');"> <B>Text for the body of the page.</B> </body>
<body onUnload="window.alert('Be sure to come back, OK?');"> <B>Text for the body of the page.</B> </body>
<form> <input type="text" onFocus="window.alert('Don\'t forget to capitalize!');"> </form>
<form> Give this box focus:<BR> <input type="text" onBlur="window.alert('Hey, Come back!');"> <BR> Then give this box to blur the first one:<BR> <input type="text"> </form>
<html> <body> <form> Are you cool?<BR> <select onChange="window.alert('Why did you change that?');"> <option selected>yes</option> <option>no</option> <option>undecided</option> </select> </form> </body> </html>
<html> <body> <form onSubmit="window.alert('Thank you!');"> What's your name?<BR> <input type="text" name="thename"> <BR> <input type="submit"> </form> </body> </html>
<a href="pr7_2.html" onMouseOver="window.status='My last project'; return true;" onMouseOut="window.status=''; return true;" > Events</a>
<form> <input type="button" value="Go Searching!" onClick="window.location='http://www.google.com';"> </form>在一個表單中,也可以使用多個鏈結按紐:
<form> <input type="button" value="Go Searching!" onClick="window.location='http://www.google.com';"> <P> <input type="button" value="Html Help!" onClick="window.location='http://www.pageresource.com';"> <P> <input type="button" value="Some Javascripts!" onClick="window.location='http://www.javascriptcity.com';"> </form>使用大量的鏈結按紐時,可以利用函式, 並以URL作為函式的參數:
<html> <head> <title>Button Links</title> <SCRIPT language="JavaScript"> <!-- function go_to(place) { window.location=place; } //--> </SCRIPT> </head> <body> <form> <input type="button" value="Go Searching!" onClick="go_to('http://www.google.com');"> <P> <input type="button" value="Html Help!" onClick="go_to('http://www.pageresource.com');"> <P> <input type="button" value="Some Javascripts!" onClick="go_to('http://www.javascriptcity.com');"> </form> <body>
例題:pr7_6.html