Refactoring Javascript code example (comparison before and after reconstruction) _ javascript tips-js tutorial

Source: Internet
Author: User
Let's look back at the Javascript scripts, which are not well written and too complicated. I have extracted and reconstructed them. If you want to learn about javascript refactoring, I can refer to this article. I am going to help you with some tutorials on how to use asp.net with Javascript today. Now let's look back at the Javascript scripts, which are not well written and too complicated. Extracted and reconstructed.
Previous 1:

The Code is as follows:


Function SelectedAll (cb ){
Cb. checked = cb. checked? False: true;
Var gv = document. getElementById ('<% = GridViewCouplets. ClientID %> ');
Var rc = gv. rows. length;
For (var I = 1; I <rc; I ++ ){
Var input = gv. rows [I]. cells [0]. getElementsByTagName ("input ");
If (input [0]. type = "checkbox" & input [0]. checked ){
Input [0]. checked = false;
Gv. rows [I]. style. backgroundColor = "";
}
Else {
Input [0]. checked = true;
Gv. rows [I]. style. backgroundColor = "#66ff33 ;";
}
}
}
Function SelectedSingle (cb ){
Var row = cb. parentNode. parentNode;
If (cb. checked ){
Row. style. backgroundColor = "#66ff33 ;";
}
Else {
Row. style. backgroundColor = "";
}
}


Restructured Javascript script:

The Code is as follows:


Function SelectedAll (cb ){
Var gv = document. getElementById ('<% = GridViewCouplets. ClientID %> ');
Var rc = gv. rows. length;
For (var I = 1; I <rc; I ++ ){
Var input = gv. rows [I]. cells [0]. getElementsByTagName ("input ");
If (input [0]. type = "checkbox ")
{
Input [0]. checked = cb. checked;
Gv. rows [I]. style. backgroundColor = input [0]. checked? "#66ff33 ;":"";
}
}
}
Function SelectedSingle (cb ){
Var row = cb. parentNode. parentNode;
Row. style. backgroundColor = cb. checked? "#66ff33 ;":"";
}


Previous 2:

The Code is as follows:


Function Check_Uncheck_All (cb ){
Var cbl = document. getElementById ("<% = CheckBoxListMusicType. ClientID %> ");
Var input = cbl. getElementsByTagName ("input ");
If (cb. checked ){
For (var I = 0; I <input. length; I ++ ){
Input [I]. checked = true;
}
}
Else {
For (var I = 0; I <input. length; I ++ ){
Input [I]. checked = false;
}
}
}


Restructured Javascript script:

The Code is as follows:


Function Check_Uncheck_All (cb ){
Var cbl = document. getElementById ("<% = CheckBoxListMusicType. ClientID %> ");
Var input = cbl. getElementsByTagName ("input ");
For (var I = 0; I <input. length; I ++ ){
Input [I]. checked = cb. checked;
}
}

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.