


JavaScript Advanced Programming Reading Notes (18) js cross-platform events_javascript skills
跨平台的事件EventUtil对象
EventUtil:
var EventUtil={
addEventHandler:function(oTarget, sEventType, fnHandler){
if(oTarget.addEventListener){
oTarget.addEventListener(sEventType,fnHandler,false);
} else if(oTarget.attachEvent){
oTarget.attachEvent("on" sEventType,fnHandler);
} else{
oTarget["on" sEventType]=fnHandler;
}
},
removeEventHandler:function(oTarget, sEventType, fnHandler){
if(oTarget.removeEventListener){
oTarget.removeEventListener(sEventType,fnHandler);
} else if(oTarget.detachEvent){
oTarget.detachEvent("on" sEventType,fnHandler);
} else{
oTarget["on" sEventType]=null;
}
},
formatEvent:function(oEvent){
var isIE=/msie/i.test(navigator.userAgent),
isWin=/win/i.test(navigator.userAgent);
if(isIE && isWin){
oEvent.charCode=(oEvent.type == "keypress")?oEvent.keyCode:0;
oEvent.eventPhase =2;
oEvent.isChar=(oEvent.charCode>0);
oEvent.pageX=oEvent.clientX document.body.scrollLeft;
oEvent.pageY=oEvent.clientY document.body.scrollTop;
oEvent.preventDefault=function(){
this.returnvalue=false;
}
if(oEvent.type == "mouseout"){
oEvent.relateTarget=oEvent.toElement;
}else if(oEvent.type=="mouseover"){
oEvent.relatedTarget=oEvent.fromElement;
}
oEvent.stopPropagation=function(){
this.cancelBubble=true;
}
oEvent.target=oEvent.srcElement;
oEvent.time=(new Date()).getTime();
}
return oEvent;
},
getEvent:function(){
if(window.event){
return this.formatEvent(window.event);
}else{
return EventUtil.getEvent.caller.arguments[0];
}
}
}
测试
<script> <br>var EventUtil={ <br>addEventHandler:function(oTarget, sEventType, fnHandler){ <br>if(oTarget.addEventListener){ <br>oTarget.addEventListener(sEventType,fnHandler,false); <br>} else if(oTarget.attachEvent){ <br>oTarget.attachEvent("on" sEventType,fnHandler); <br>} else{ <br>oTarget["on" sEventType]=fnHandler; <br>} <br>}, <br>removeEventHandler:function(oTarget, sEventType, fnHandler){ <br>if(oTarget.removeEventListener){ <br>oTarget.removeEventListener(sEventType,fnHandler); <br>} else if(oTarget.detachEvent){ <br>oTarget.detachEvent("on" sEventType,fnHandler); <br>} else{ <br>oTarget["on" sEventType]=null; <br>} <br>}, <br>formatEvent:function(oEvent){ <br>var isIE=/msie/i.test(navigator.userAgent), <br>isWin=/win/i.test(navigator.userAgent); <br>if(isIE && isWin){ <br>oEvent.charCode=(oEvent.type == "keypress")?oEvent.keyCode:0; <br>oEvent.eventPhase =2; <br>oEvent.isChar=(oEvent.charCode>0); <br>oEvent.pageX=oEvent.clientX document.body.scrollLeft; <br>oEvent.pageY=oEvent.clientY document.body.scrollTop; <br>oEvent.preventDefault=function(){ <br>this.returnvalue=false; <br>} <br>if(oEvent.type == "mouseout"){ <br>oEvent.relateTarget=oEvent.toElement; <br>}else if(oEvent.type=="mouseover"){ <br>oEvent.relatedTarget=oEvent.fromElement; <br>} <br>oEvent.stopPropagation=function(){ <br>this.cancelBubble=true; <br>} <br>oEvent.target=oEvent.srcElement; <br>oEvent.time=(new Date()).getTime(); <br>} <br>return oEvent; <br>}, <br>getEvent:function(){ <br>if(window.event){ <br>return this.formatEvent(window.event); <br>}else{ <br>return EventUtil.getEvent.caller.arguments[0]; <br>} <br>} <br>} <br>EventUtil.addEventHandler(window,"load",function(){ <br>var oDiv=document.getElementById("div1"); <br>EventUtil.addEventHandler(oDiv,"mouseover",handleEvent); <br>EventUtil.addEventHandler(oDiv,"mouseout",handleEvent); <br>EventUtil.addEventHandler(oDiv,"mousedown",handleEvent); <br>EventUtil.addEventHandler(oDiv,"mouseup",handleEvent); <br>EventUtil.addEventHandler(oDiv,"click",handleEvent); <br>EventUtil.addEventHandler(oDiv,"dblclick",handleEvent); <br>}); <br>function handleEvent(){ <br>var oEvent=EventUtil.getEvent(); <br>var oTextbox=document.getElementById("txt1"); <br>oTextbox.value ="n>" oEvent.type; <br>oTextbox.value ="n target is " oEvent.target.tagName; <br>if(oEvent.relatedTarget){ <br>oTextbox.value ="n relateTarget is " oEvent.relatedTarget.tagName; <br>} <br>} <br></script>
Use your mouse to click and double click the red square.
// 0);
oEvent.pageX=oEvent.clientX document.body.scrollLeft;
oEvent.pageY=oEvent.clientY document.body.scrollTop;
oEvent.preventDefault=function(){
this.returnvalue=false;
}
if(oEvent.type == "mouseout"){
oEvent.relateTarget=oEvent.toElement;
}else if(oEvent.type=="mouseover"){
oEvent.relatedTarget=oEvent.fromElement;
}
oEvent.stopPropagation=function(){
this.cancelBubble=true;
}
oEvent.target=oEvent.srcElement;
oEvent.time=(new Date()).getTime();
}
return oEvent;
},
getEvent:function(){
if(window.event){
return this.formatEvent(window.event);
}else{
return EventUtil.getEvent.caller.arguments[0];
}
}
}
window.onload=function(){
var oDiv=document.getElementById("div1");
EventUtil.addEventHandler(oDiv,"mouseover",handleEvent);
EventUtil.addEventHandler(oDiv,"mouseout",handleEvent);
EventUtil.addEventHandler(oDiv,"mousedown",handleEvent);
EventUtil.addEventHandler(oDiv,"mouseup",handleEvent);
EventUtil.addEventHandler(oDiv,"click",handleEvent);
EventUtil.addEventHandler(oDiv,"dblclick",handleEvent);
}
function handleEvent(){
var oEvent=EventUtil.getEvent();
var oTextbox=document.getElementById("txt1");
oTextbox.innerHTML ="
>" oEvent.type;
oTextbox.innerHTML ="
target is " oEvent.target.tagName;
if(oEvent.relatedTarget){
oTextbox.innerHTML ="
relateTarget is " oEvent.relatedTarget.tagName;
}
}
// ]]>
Use your mouse to click and double click the red square.
Test
作者:Artwl
出处:http://artwl.cnblogs.com

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to perform data verification on C++ code? Data verification is a very important part when writing C++ code. By verifying the data entered by the user, the robustness and security of the program can be enhanced. This article will introduce some common data verification methods and techniques to help readers effectively verify data in C++ code. Input data type check Before processing the data input by the user, first check whether the type of the input data meets the requirements. For example, if you need to receive integer input from the user, you need to ensure that the user input is

As a fast and efficient programming language, Go language has been widely used in back-end development. However, with the continuous development of Go language, more and more developers are beginning to try to use Go language for GUI interface development in the front-end field. This article will introduce readers to how to use Go language for cross-platform GUI interface design, and provide specific code examples to help readers get started and apply it better. 1. Introduction to Go language GUI development GUI (GraphicalUserInterface, for graphics)

Go is an open source, cross-platform programming language known for its simplicity, speed, and concurrency. It is used in a wide range of applications ranging from simple scripts to large distributed systems. Its main advantages include cross-platform, open source, simplicity, speed and concurrency. For example, Go makes it easy to build a simple HTTP server or concurrent crawler.

PHP cross-platform development trends: progressive web applications, responsive design, cloud computing integration. Technology outlook: continued development of PHP framework, artificial intelligence integration, and IoT support. Practical case: Laravel builds cross-platform progressive web applications.

C++ functions play a vital role in cross-platform GUI development, providing cross-platform APIs to create and manage GUIs. These APIs include SFML, Qt, and GLFW, which provide common functions to operate windows, controls, and events. These functions allow developers to build consistent GUI experiences across different operating systems, simplifying multi-platform development and enabling applications that run seamlessly on various platforms.

Answer: In cross-platform development, the PHP framework improves efficiency by making code reusable, improving productivity, and shortening development time. Details: Code reusable: Provides pre-built components and classes to reduce repetitive code writing. Increase productivity: Automate tedious tasks such as database interactions, allowing developers to focus on core functionality. Faster development time: Pre-built components and automated features speed up development without having to code from scratch.

Integration of Vue.js and Dart language, ideas for building cross-platform mobile applications In the field of mobile application development, cross-platform development frameworks have received more and more attention. Vue.js is a JavaScript framework for building user interfaces, while Dart is a language developed by Google for building cross-platform applications. This article will explore how to integrate Vue.js with the Dart language to build cross-platform mobile applications. 1. Introduction to Vue.js Vue.js is considered a lightweight

Best practices for creating cross-platform graphics applications: Choose a cross-platform framework: Qt, wxWidgets, or GLFW Create portable code: Use portable C++ standards to avoid platform-specific code Optimize performance: Use hardware-accelerated graphics APIs to avoid unnecessary Memory manipulation, optimized layout handling Multi-platform compatibility: use appropriate compiler flags, test applications, provide platform-specific resources
