Home  >  Article  >  Web Front-end  >  js imitates jquery writing sample code_javascript skills

js imitates jquery writing sample code_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:32:091054browse

Test code:

Copy code The code is as follows:

(function(){
var p=new PEvent(document);
p.click(function() {
//alert("click");
//alert(p.style);
var html= "";
for ( var item in document) {
html =item ':' document[item] "rn";
}
//alert(html);
}) ;
p.dblclick(function() {
alert("double-click");
});
p.contextmenu(function(event) {
try{
var x =event.clientX;
var y=event.clientY;
var menu=g("menu");

//Judge coordinates
var width=document.body.clientWidth;
var height=document.body.clientHeight;
x=(x menu.clientWidth)>=width?width-menu.clientWidth:x;
y=(y menu.clientHeight)>= height?height-menu.clientHeight:y;

//alert("Visible height: " height ", mouse height: " y);
menu.style.top=y "px";
menu.style.left=x "px";
menu.style.display="block";

}catch(e){
alert(e);
}
return false;
});
function PEvent(dom){

this.x=function() {
this.style.css=dom.style;
}

this.click=function(fn){
dom.onclick=fn;
this.x();
}

this.dblclick= function(fn){
dom.ondblclick=fn;
}

this.contextmenu=function(fn){
dom.oncontextmenu=fn;
}

this.style=new Po();

};

function Po() {
this.name=new Object();
this.id=new Object();
this.css=new Object();
}
})();
function g(id){
return document.getElementById(id);
}

In jquery, when handling events, you can write anonymous methods, for example:
Copy code The code is as follows:

obj.click(function(){
alert("hello");
});

Appeal this form.
When passing parameters in a method, you can pass the fun method.
can be called like this:
Copy code The code is as follows:

this .dblclick=function(fn){
dom.ondblclick=fn;
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn