首页 > 代码库 > 响应事件的示例
响应事件的示例
示例 1 onm ousedown, onm ouseup
<script>
function textcolor(cursor, i){
if(i ==0)
cursor.style.color ="#0000FF";
else
cursor.style.color ="#E7D745";
}
</script>
<h2 onm ouseup="textcolor(this,1)"
onm ousedown="textcolor(this,0)">
按下、松开颜色改变的文本
</h2>
示例 2 onm ouseover,onmouseout
<script>
function changeImage(img, i){
if(i ==0)
img.src ="images/temp0.jpg";
else
img.src ="images/temp1.jpg";
}
</script>
<img src="images/temp0.jpg" border="0" width="200" height="220"
onm ouseOver="changeImage(this,1)"
onm ouseOut="changeImage(this,0)">
示例3 计数器(countdown)
<div id="container">
<div id="inputArea">
</div>
<h1 id="time">0:00</h1>
</div>
<script>
// two global variables
var secondsRemaining;
var intervalHandle;
function resetPage(){
document.getElementById("inputArea").style.display ="block";
}
function tick(){
// grab the h1
var timeDisplay = document.getElementById("time");
// turn seconds into mm:ss
var min =Math.floor(secondsRemaining /60);// floor功能: 返回比参数小的最大整数
var sec = secondsRemaining -(min *60);
// add a leading zero (as a string value) if seconds less than 10
if(sec <10){
sec ="0"+ sec;
}
// concatenate with colon
var message = min +":"+ sec;
// now change the display
timeDisplay.innerHTML = message;
// stop if down to zero
if(secondsRemaining ===0){
alert("Done!");
clearInterval(intervalHandle);
resetPage();
}
// subtract from seconds remaining
secondsRemaining--;
}
function startCountdown(){
// get contents of the "minutes" text box
var minutes = document.getElementById("minutes").value;
// check if not a number
if(isNaN(minutes)){
alert("Please enter a number!");
return;
}
// how many seconds?
secondsRemaining = minutes *60;
// every second, call the "tick" function
intervalHandle = setInterval(tick,1000);
// hide the form
document.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 button
var 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>
来自为知笔记(Wiz)
响应事件的示例
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。