Javascript事件實例詳解
更新時間:2013年11月06日 15:00:21 作者:
本文實例當(dāng)你單擊頁面上的任何位置都會彈出“a”,正是運用了document的特性
document是位于html標(biāo)簽之上的,可以說是權(quán)力最大的。下面的實例當(dāng)你單擊頁面上的任何位置都會彈出“a”,正是運用了document的特性。
<script>
document.onclick=function(){
alert('a');
};
</script>
獲取鼠標(biāo)位置clientX,clientY---注意這里僅僅只是可視區(qū)的鼠標(biāo)位置
<script>
document.onclick=function(ev){
if(ev)
{
alert(ev.clientX+','+ev.clientY);
}
else{
alert(event.clientX+','+event.clentY);
};
};
</script>
或者
<script>
document.onclick=function(ev){
/* if(ev)
{
alert(ev.clientX+','+ev.clientY);
}
else{
alert(event.clientX+','+event.clentY);
};
};*/
var oEvent=ev||event;
alert(oEvent.clientX+','+oEvent.clientY);
};
</script>
事件冒泡---一層一層疊加的元素在一起,形成事件冒泡,比如下面的例子:document的最大范圍影響了div的響應(yīng)。
<script>
window.onload=function(){
var obtn=document.getElementById('btn1');
var odiv=document.getElementById('div1');
obtn.onclick=function(ev){
var oEvent=ev||event;
odiv.style.display='block';
oEvent.cancelBubble=true;//清除冒泡
};
document.onclick=function(){
odiv.style.display='none';
};
};
</script>
</head>
<body>
<input type="button" value="顯示" id="btn1"/>
<div id="div1" style="width:100px;height:150px;background:#ccc;"></div>
</body>
鼠標(biāo)移動---在可視區(qū)有效
<title>鼠標(biāo)移動</title>
<script>
window.onmousemove=function(ev){
var oEvent=ev||event;
var odiv=document.getElementById('div1');
odiv.style.left=oEvent.clientX+'px';
odiv.style.top=oEvent.clientY+'px';
};
</script>
</head>
<body>
<div id="div1" style="width:50px;height:50px;background:blue;position:absolute;"></div>
</body>
鍵盤改變位置和方向---通過keycode獲取鍵盤的鍵值來執(zhí)行相應(yīng)的操作。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
#div1 {width:100px; height:100px; background:#CCC; position:absolute;}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標(biāo)題文檔</title>
<script>
document.onkeydown=function (ev)
{
var oEvent=ev||event;
var oDiv=document.getElementById('div1');
//← 37
//右 39
if(oEvent.keyCode==37)
{
oDiv.style.left=oDiv.offsetLeft-10+'px';
}
else if(oEvent.keyCode==39)
{
oDiv.style.left=oDiv.offsetLeft+10+'px';
}
};
</script>
</head>
<body>
<div id="div1"></div>
</body>
</html>
鼠標(biāo)跟隨小尾巴
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
div {width:10px; height:10px; background:red; position:absolute;}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標(biāo)題文檔</title>
<script>
window.onload=function ()
{
var aDiv=document.getElementsByTagName('div');
var i=0;
document.onmousemove=function (ev)
{
var oEvent=ev||event;
for(i=aDiv.length-1;i>0;i--)
{
aDiv[i].style.left=aDiv[i-1].style.left;
aDiv[i].style.top=aDiv[i-1].style.top;
}
aDiv[0].style.left=oEvent.clientX+'px';
aDiv[0].style.top=oEvent.clientY+'px';
};
};
</script>
</head>
<body>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</body>
</html>
keycode
<script>
document.onkeydown=function (ev)
{
var oEvent=ev||event;
alert(oEvent.keyCode);
};
</script>
ctrlKey---可以通過ctrl+enter組合鍵來提交內(nèi)容
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標(biāo)題文檔</title>
<script>
window.onload=function ()
{
var oBtn=document.getElementById('btn1');
var oTxt1=document.getElementById('txt1');
var oTxt2=document.getElementById('txt2');
oBtn.onclick=function ()
{
oTxt1.value+=oTxt2.value+'\n';
oTxt2.value='';
};
oTxt2.onkeydown=function (ev)
{
var oEvent=ev||event;
if(oEvent.ctrlKey && oEvent.keyCode==13)
{
oTxt1.value+=oTxt2.value+'\n';
oTxt2.value='';
}
};
};
</script>
</head>
<body>
<textarea id="txt1" rows="10" cols="40"></textarea><br />
<input id="txt2" type="text" />
<input id="btn1" type="button" value="留言" />
</body>
</html>
復(fù)制代碼 代碼如下:
<script>
document.onclick=function(){
alert('a');
};
</script>
獲取鼠標(biāo)位置clientX,clientY---注意這里僅僅只是可視區(qū)的鼠標(biāo)位置
復(fù)制代碼 代碼如下:
<script>
document.onclick=function(ev){
if(ev)
{
alert(ev.clientX+','+ev.clientY);
}
else{
alert(event.clientX+','+event.clentY);
};
};
</script>
或者
復(fù)制代碼 代碼如下:
<script>
document.onclick=function(ev){
/* if(ev)
{
alert(ev.clientX+','+ev.clientY);
}
else{
alert(event.clientX+','+event.clentY);
};
};*/
var oEvent=ev||event;
alert(oEvent.clientX+','+oEvent.clientY);
};
</script>
事件冒泡---一層一層疊加的元素在一起,形成事件冒泡,比如下面的例子:document的最大范圍影響了div的響應(yīng)。
復(fù)制代碼 代碼如下:
<script>
window.onload=function(){
var obtn=document.getElementById('btn1');
var odiv=document.getElementById('div1');
obtn.onclick=function(ev){
var oEvent=ev||event;
odiv.style.display='block';
oEvent.cancelBubble=true;//清除冒泡
};
document.onclick=function(){
odiv.style.display='none';
};
};
</script>
</head>
<body>
<input type="button" value="顯示" id="btn1"/>
<div id="div1" style="width:100px;height:150px;background:#ccc;"></div>
</body>
鼠標(biāo)移動---在可視區(qū)有效
復(fù)制代碼 代碼如下:
<title>鼠標(biāo)移動</title>
<script>
window.onmousemove=function(ev){
var oEvent=ev||event;
var odiv=document.getElementById('div1');
odiv.style.left=oEvent.clientX+'px';
odiv.style.top=oEvent.clientY+'px';
};
</script>
</head>
<body>
<div id="div1" style="width:50px;height:50px;background:blue;position:absolute;"></div>
</body>
鍵盤改變位置和方向---通過keycode獲取鍵盤的鍵值來執(zhí)行相應(yīng)的操作。
復(fù)制代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
#div1 {width:100px; height:100px; background:#CCC; position:absolute;}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標(biāo)題文檔</title>
<script>
document.onkeydown=function (ev)
{
var oEvent=ev||event;
var oDiv=document.getElementById('div1');
//← 37
//右 39
if(oEvent.keyCode==37)
{
oDiv.style.left=oDiv.offsetLeft-10+'px';
}
else if(oEvent.keyCode==39)
{
oDiv.style.left=oDiv.offsetLeft+10+'px';
}
};
</script>
</head>
<body>
<div id="div1"></div>
</body>
</html>
鼠標(biāo)跟隨小尾巴
復(fù)制代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
div {width:10px; height:10px; background:red; position:absolute;}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標(biāo)題文檔</title>
<script>
window.onload=function ()
{
var aDiv=document.getElementsByTagName('div');
var i=0;
document.onmousemove=function (ev)
{
var oEvent=ev||event;
for(i=aDiv.length-1;i>0;i--)
{
aDiv[i].style.left=aDiv[i-1].style.left;
aDiv[i].style.top=aDiv[i-1].style.top;
}
aDiv[0].style.left=oEvent.clientX+'px';
aDiv[0].style.top=oEvent.clientY+'px';
};
};
</script>
</head>
<body>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</body>
</html>
keycode
復(fù)制代碼 代碼如下:
<script>
document.onkeydown=function (ev)
{
var oEvent=ev||event;
alert(oEvent.keyCode);
};
</script>
ctrlKey---可以通過ctrl+enter組合鍵來提交內(nèi)容
復(fù)制代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標(biāo)題文檔</title>
<script>
window.onload=function ()
{
var oBtn=document.getElementById('btn1');
var oTxt1=document.getElementById('txt1');
var oTxt2=document.getElementById('txt2');
oBtn.onclick=function ()
{
oTxt1.value+=oTxt2.value+'\n';
oTxt2.value='';
};
oTxt2.onkeydown=function (ev)
{
var oEvent=ev||event;
if(oEvent.ctrlKey && oEvent.keyCode==13)
{
oTxt1.value+=oTxt2.value+'\n';
oTxt2.value='';
}
};
};
</script>
</head>
<body>
<textarea id="txt1" rows="10" cols="40"></textarea><br />
<input id="txt2" type="text" />
<input id="btn1" type="button" value="留言" />
</body>
</html>
您可能感興趣的文章:
- javascript事件函數(shù)中獲得事件源的兩種不錯方法
- javascript中的事件代理初探
- java開發(fā)gui教程之jframe監(jiān)聽窗體大小變化事件和jframe創(chuàng)建窗體
- javascript頁面加載完執(zhí)行事件代碼
- javaScript 頁面自動加載事件詳解
- JavaScript在for循環(huán)中綁定事件解決事件參數(shù)不同的情況
- javascript計時器事件使用詳解
- javascript使用onclick事件改變選中行的顏色
- javascript 按鍵事件(兼容各瀏覽器)
- 分享JavaScript獲取網(wǎng)頁關(guān)閉與取消關(guān)閉的事件
- javascript阻止瀏覽器后退事件防止誤操作清空表單
- IE8的JavaScript點擊事件(onclick)不兼容的解決方法
- javascript阻止scroll事件多次執(zhí)行的思路及實現(xiàn)
- jQuery javaScript捕獲回車事件(示例代碼)
- JavaScript的事件綁定(方便不支持js的時候)
- JavaScript加強之自定義event事件
- javascript簡單事件處理和with用法介紹
- java隨機事件分發(fā)器示例
相關(guān)文章
客戶端腳本中常常出現(xiàn)的一些問題和調(diào)試技巧
客戶端腳本中常常出現(xiàn)的一些問題和調(diào)試技巧...2007-01-01Javascript WebSocket使用實例介紹(簡明入門教程)
網(wǎng)絡(luò)套接字是下一代WEB應(yīng)用程序雙向通信技術(shù),它是基于一個獨立的socket并且需要客戶端瀏覽器支持HTML52014-04-04JavaScript創(chuàng)建、讀取和刪除cookie
通過本文你將粗略的明白cookie是什么,如何通過js創(chuàng)建/存儲以及獲取cookie,如何讓cookie過期來刪除cookie2019-09-09Javascript基礎(chǔ)教程之?dāng)?shù)組 array
Array是JavaScript中常用的類型,并且JavaScript中的數(shù)組和其他語言的數(shù)組有比較大的區(qū)別。JavaScript中數(shù)組中存放的數(shù)據(jù)類型不一定相同,而且數(shù)組的長度也是可改變的。2015-01-01