标签:
<!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>event</title> <script> document.onclick=function (ev) { //兼容的写法 var oEvent=ev||event; alert(oEvent.clientX+‘, ‘+oEvent.clientY); //IE下兼容的写法 //alert(event.clientX+‘, ‘+event.clientY); //FF兼容的写法 //alert(ev.clientX+‘, ‘+ev.clientY); /*if(ev) { alert(ev.clientX+‘, ‘+ev.clientY); } else { alert(event.clientX+‘, ‘+event.clientY); }*/ }; </script> </head> <body> </body> </html>
<!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>无标题文档</title> <script> document.onkeydown=function (ev) { var oEvent=ev||event; alert(oEvent.keyCode); }; </script> </head> <body> </body> </html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html onclick="alert(‘html‘);" xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body onclick="alert(‘body‘);"> <div style="width:300px; height:300px; background:red;" onclick="alert(this.style.background);"> <div style="width:200px; height:200px; background:green;" onclick="alert(this.style.background);"> <div style="width:100px; height:100px; background:#CCC;" onclick="alert(this.style.background);"> </div> </div> </div> </body> </html>
oEvent.cancelBubble=true;差入到以上函数执行内部则终止则实现终止冒泡!
DOM事件流
<!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:red; position:absolute;} </style> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <script> document.onmousemove=function (ev) { //获取客户端 get Oevent var oEvent=ev||event; //获取div get div var oDiv=document.getElementById(‘div1‘); //获取滚动条滚动时的高和左边距 var scrollTop=document.documentElement.scrollTop||document.body.scrollTop; var scrollLeft=document.documentElement.scrollLeft||document.body.scrollLeft; //动态赋予div左和高 oDiv.style.left=oEvent.clientX+scrollLeft+‘px‘; oDiv.style.top=oEvent.clientY+scrollTop+‘px‘; }; </script> </head> <body> <div id="div1"> </div> </body> </html>
<!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>无标题文档</title> <script> document.onclick=function () { //处理兼容问题 var scrollTop=document.documentElement.scrollTop||document.body.scrollTop; //输出滚动后离浏览器上边距 alert(scrollTop); }; </script> </head> <body style="height:2000px;"> </body> </html>
<!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>无标题文档</title> <style> body{background:#333;} div {width:10px; height:10px; background:#FFF; position:absolute;} </style> <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--) { //数组左边的div等于离客户端最近的div 左上边距 aDiv[i].style.left=aDiv[i-1].style.left; aDiv[i].style.top=aDiv[i-1].style.top; } //adv的左上等于客户端点击的边距; 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> </ht
<!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>无标题文档</title> <style> #div1{ width:100px; height:100px; position:absolute; background:#F00; top:0; left:0;} </style> <script> window.onload=function(){ var oDiv=document.getElementById(‘div1‘); //把键值默认为假 var i=r=t=b=false; //监听 键盘按下 document.addEventListener(‘keydown‘,function(ev){ var oEvent=ev||event; // 判断键值数字 如何是则为真 switch(oEvent.keyCode){ case 37: i=true; break; case 38: r=true; break; case 39: t=true; break; case 40: b=true; break; } },false); //监听 键盘抬起 document.addEventListener(‘keyup‘,function(ev){ var oEvent=ev||event; //判断键值 是真则为假 switch(oEvent.keyCode){ case 37: i=false; break; case 38: r=false; break; case 39: t=false; break; case 40: b=false; break; } },false); setInterval(function(){ //分配写入各个键值的作用 if(i) oDiv.style.left=oDiv.offsetLeft-10+‘px‘; if(t) oDiv.style.left = oDiv.offsetLeft + 10 + "px"; if(r) oDiv.style.top = oDiv.offsetTop - 10 + "px"; if(b) oDiv.style.top = oDiv.offsetTop + 10 + "px"; },1) }; </script> </head> <body> <div id="div1"> </div> </body> </html>
<!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>无标题文档</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.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>
<!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>无标题文档</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>
标签:
原文地址:http://www.cnblogs.com/hack-ing/p/5558250.html