Jquery Object-Oriented Programming

Source: Internet
Author: User

Recently, I am engaged in some front-end interactions and have a new understanding of js object-oriented programming;


The scenario is as follows:

 

The page now has three areas: A, B, and C. Click different tabs to switch to different layers. Take a look at the HTML code:

[Html]
<Div class = "m" id = "switch" data-widget = "tabs">
<Div class = "fore1 curr" data-widget = "tab-item">
<Div class = "smt"> A </div>
<Div class = "smc" data-widget = "tab-content">
</Div>
</Div>
<Div class = "fore2" data-widget = "tab-item">
<Div class = "smt"> B </div>
<Div class = "smc" data-widget = "tab-content">
</Div>
</Div>
<Div class = "fore3" data-widget = "tab-item">
<Div class = "smt"> C </div>
<Div class = "smc" data-widget = "tab-content">
</Div>
</Div>
</Div>
</Div>

The tab content is displayed and hidden. The style css controls the following:
[Css]
# Switch. curr. smc {
Display: block;
}
# Switch. smc {
Display: none;
Overflow: visible;
}

When you click the current tab, add the class "curr ";

If you don't talk too much about it, let's take a look at how to use programming-oriented ideas;

Because there are a lot of interactions in layer switches, I will consider making an object:

[Javascript]
Var a = {obj: $ ("# switch"), func: function (){
Var p = a. obj. find (". smt ");
P. bind ("click", function (){
P. parent (). removeClass ("curr ");
JQuery (this). parent (). addClass ("curr ");
//
If (jQuery (this). attr ("id") = ""){
Load ();
}
// B
If (jQuery (this). attr ("id") = "B "){
Load ();
}
// C
If (jQuery (this). attr ("id") = "C "){
Load ();
}
})
}, Init: function (){
A. func ();
}};
A. init ();
There are obj objects, func functions, and func, which are easy to implement click switching and hide.

For the response speed of the page, Area B and C may be loaded only after clicking. We identify the ID to judge the loading;

If you do not need to load each click, you can define an attribute, change the value when you click it for the first time, and click again to determine whether to load the attribute.


[Javascript]
Var a = {obj: $ ("# switch"), func: function (){
Var p = a. obj. find (". smt ");
P. bind ("click", function (){
P. parent (). removeClass ("curr ");
JQuery (this). parent (). addClass ("curr ");
//
If (jQuery (this). attr ("id") = "A" & jQuery (this). attr ("data-flag") = "1 "){
Load ();
JQuery (this). attr ("data-flag", "0 ");
}
// B
If (jQuery (this). attr ("id") = "B" & jQuery (this). attr ("data-flag") = "1 "){
Load ();
JQuery (this). attr ("data-flag", "0 ");
}
// C
If (jQuery (this). attr ("id") = "C" & jQuery (this). attr ("data-flag") = "1 "){
Load ();
JQuery (this). attr ("data-flag", "0 ");
}
})
}, Init: function (){
A. func ();
}};

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.