JS imitate jquery's writing example code _javascript tips

Source: Internet
Author: User
Test code:
Copy Code code 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]+ ' \ 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");

Judging 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 ("Visual 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 anonymous methods when handling events, such as:
Copy Code code as follows:

Obj.click (function () {
Alert ("Hello");
});

Appeal in this form.
When the method passes the parameter, the fun method can be passed.
Call, you can call it like this:
Copy Code code as follows:

This.dblclick=function (FN) {
DOM.ONDBLCLICK=FN;
}

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.