Js imitates jquery's Writing Method

Source: Internet
Author: User
[Javascript] (function () {varpnewPEvent (document); p. click (function () {// alert (& quot; click & quot;); // alert (p. style); varhtml & quot; for (varitemindocument) {html + item

[Javascript]
(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] + "\ r \ n ";
}
// 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 ");

// Determine 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 );
}

(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] + "\ r \ n ";
}
// 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 ");
 
// Determine 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, you can write events anonymously, for example:

Obj. click (function (){

Alert ("hello ");


});

Appeal form.

 


When passing parameters in a method, you can pass the fun method.

You can call it as follows:


[Javascript]
This. dblclick = function (fn ){
Dom. ondblclick = fn;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.