Home  >  Article  >  Web Front-end  >  Application of JQuery custom events JQuery best practices_jquery

Application of JQuery custom events JQuery best practices_jquery

WBOY
WBOYOriginal
2016-05-16 18:22:19842browse

Knowledge points:

1. Custom events and event subscriptions

2. Use of trigger, bind, and unbind methods

3. Customization with namespace Event

Paste the following content into the txt document and then paste it directly into the firebug console and run it.

Copy code The code is as follows:

/*
JQuery custom event application- Javascript OO
There is a handsome guy named Levin, who works in the front-end design department of a company. His supervisor is handsome guy A, and his manager is pretty girl B.
The leaders wanted to take the time to take a look at this guy’s work every week, so. . .
Levin has to use Google Doc to write a summary of last week's work every Monday, and is required to notify A and B on MSN after finishing the writing. . .
Levin is very happy to do the weekly summary, but he made a suggestion:
Since I have written it in Google Doc, if you like to read it, you can use Google Reader to subscribe to my document updates. I don’t like to use Microsoft’s MSN. . . .

Here...A and B had crows floating above their heads for a few seconds, but they readily agreed.
*/

//This is classmate Levin
Copy code The code is as follows:

var Levin={name:"Levin"};
Levin=$(Levin);


//Write a weekly summary every week
Copy code The code is as follows:

Levin.bind("evt_weeklyReport",function(evt){
alert(this.name "Shout: Yeah! The weekly summary has been written.");
});


//Supervisor A
Copy code The code is as follows:

var A={name:"Handsome Guy A"};

//He wants to subscribe to Levin’s weekly summary
Copy the code The code is as follows:

A.rssLevin=function(){
Levin.bind("evt_weeklyReport.fromA",function(evt,data){
alert(A.name ": Well, not bad, Levin is quite positive) ! ");
})
};
A.rssLevin();

//Manager B
Copy code The code is as follows:

var B={name:"Meimei B"};


// She also subscribes to Levin's weekly summary
Copy code The code is as follows:

B.rssLevin=function (){
Levin.bind("evt_weeklyReport.fromB",function(evt,data){
alert(B.name ": Weekly summary will be read later, first take a look at the website he mentioned");
window.location=evt.site;
})
};
B.rssLevin();


//Every time Levin writes After the weekly summary, I will publish it using google doc
Copy the code The code is as follows:

Levin. trigger({type:"evt_weeklyReport",site:"http://jb51.net"});


//Manager B suddenly went to Sohu one day and stopped subscribing Levin's weekly summary...
[code]
B.unRssLevin=function(){
alert("I'm going to Sohu to be the vice president, haha");
Levin.unbind( "evt_weeklyReport.fromB");
return true;
}();
[html]

//Although B has changed jobs, the weekly summary still needs to be released on time. . .
Copy code The code is as follows:

Levin.trigger({type: "evt_weeklyReport",site :"http://jb51.net"});
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