示例 1 onmousedown, onmouseup
<script>function textcolor(cursor, i){if(i ==0)cursor.style.color ="#0000FF";elsecursor.style.color ="#E7D745";}</script><h2 onmouseup="textcolor(this,1)"onmousedown="textcolor(this,0)">按下、松开颜色改变的文本</h2>
示例 2 onmouseover,onmouseout
<script>function changeImage(img, i){if(i ==0)img.src ="images/temp0.jpg";elseimg.src ="images/temp1.jpg";}</script><img src="images/temp0.jpg" border="0" width="200" height="220"onmouseOver="changeImage(this,1)"onmouseOut="changeImage(this,0)">
示例3 计数器(countdown)
<div id="container"><div id="inputArea"></div><h1 id="time">0:00</h1></div><script>// two global variablesvar secondsRemaining;var intervalHandle;function resetPage(){document.getElementById("inputArea").style.display ="block";}function tick(){// grab the h1var timeDisplay = document.getElementById("time");// turn seconds into mm:ssvar min =Math.floor(secondsRemaining /60);// floor功能: 返回比参数小的最大整数var sec = secondsRemaining -(min *60);// add a leading zero (as a string value) if seconds less than 10if(sec <10){sec ="0"+ sec;}// concatenate with colonvar message = min +":"+ sec;// now change the displaytimeDisplay.innerHTML = message;// stop if down to zeroif(secondsRemaining ===0){alert("Done!");clearInterval(intervalHandle);resetPage();}// subtract from seconds remainingsecondsRemaining--;}function startCountdown(){// get contents of the "minutes" text boxvar minutes = document.getElementById("minutes").value;// check if not a numberif(isNaN(minutes)){alert("Please enter a number!");return;}// how many seconds?secondsRemaining = minutes *60;// every second, call the "tick" functionintervalHandle = setInterval(tick,1000);// hide the formdocument.getElementById("inputArea").style.display ="none";}// as soon as the page is loaded...window.onload =function(){// create input text box and give it an id of "minutes"var inputMinutes = document.createElement("input");inputMinutes.setAttribute("id","minutes");inputMinutes.setAttribute("type","text");// create a buttonvar startButton = document.createElement("input");startButton.setAttribute("type","button");startButton.setAttribute("value","Start Countdown");startButton.onclick =function(){startCountdown();};// add to the DOM, to the div called "inputArea"document.getElementById("inputArea").appendChild(inputMinutes);document.getElementById("inputArea").appendChild(startButton);};</script>