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;
}
}