响应事件的示例

示例 1 onmousedown, onmouseup

  1. <script>
  2. function textcolor(cursor, i){
  3. if(i ==0)
  4. cursor.style.color ="#0000FF";
  5. else
  6. cursor.style.color ="#E7D745";
  7. }
  8. </script>
  9. <h2 onmouseup="textcolor(this,1)"
  10. onmousedown="textcolor(this,0)">
  11. 按下、松开颜色改变的文本
  12. </h2>
 
 
示例 2 onmouseover,onmouseout
  1. <script>
  2. function changeImage(img, i){
  3. if(i ==0)
  4. img.src ="images/temp0.jpg";
  5. else
  6. img.src ="images/temp1.jpg";
  7. }
  8. </script>
  9. <img src="images/temp0.jpg" border="0" width="200" height="220"
  10. onmouseOver="changeImage(this,1)"
  11. onmouseOut="changeImage(this,0)">
 
示例3  计数器(countdown)
  1. <div id="container">
  2. <div id="inputArea">
  3. </div>
  4. <h1 id="time">0:00</h1>
  5. </div>
  6. <script>
  7. // two global variables
  8. var secondsRemaining;
  9. var intervalHandle;
  10. function resetPage(){
  11. document.getElementById("inputArea").style.display ="block";
  12. }
  13. function tick(){
  14. // grab the h1
  15. var timeDisplay = document.getElementById("time");
  16. // turn seconds into mm:ss
  17. var min =Math.floor(secondsRemaining /60);// floor功能: 返回比参数小的最大整数
  18. var sec = secondsRemaining -(min *60);
  19. // add a leading zero (as a string value) if seconds less than 10
  20. if(sec <10){
  21. sec ="0"+ sec;
  22. }
  23. // concatenate with colon
  24. var message = min +":"+ sec;
  25. // now change the display
  26. timeDisplay.innerHTML = message;
  27. // stop if down to zero
  28. if(secondsRemaining ===0){
  29. alert("Done!");
  30. clearInterval(intervalHandle);
  31. resetPage();
  32. }
  33. // subtract from seconds remaining
  34. secondsRemaining--;
  35. }
  36. function startCountdown(){
  37. // get contents of the "minutes" text box
  38. var minutes = document.getElementById("minutes").value;
  39. // check if not a number
  40. if(isNaN(minutes)){
  41. alert("Please enter a number!");
  42. return;
  43. }
  44. // how many seconds?
  45. secondsRemaining = minutes *60;
  46. // every second, call the "tick" function
  47. intervalHandle = setInterval(tick,1000);
  48. // hide the form
  49. document.getElementById("inputArea").style.display ="none";
  50. }
  51. // as soon as the page is loaded...
  52. window.onload =function(){
  53. // create input text box and give it an id of "minutes"
  54. var inputMinutes = document.createElement("input");
  55. inputMinutes.setAttribute("id","minutes");
  56. inputMinutes.setAttribute("type","text");
  57. // create a button
  58. var startButton = document.createElement("input");
  59. startButton.setAttribute("type","button");
  60. startButton.setAttribute("value","Start Countdown");
  61. startButton.onclick =function(){
  62. startCountdown();
  63. };
  64. // add to the DOM, to the div called "inputArea"
  65. document.getElementById("inputArea").appendChild(inputMinutes);
  66. document.getElementById("inputArea").appendChild(startButton);
  67. };
  68. </script>
 
 





posted @ 2016-10-21 15:05  Jener_Yan  阅读(356)  评论(0编辑  收藏  举报