Today I have made several tutorials on how to use asp.net with Javascript. Now let's look back at the Javascript scripts, which are not well written and too complicated. Extracted and reconstructed.
Previous 1:
Copy codeThe 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:Copy codeThe 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:Copy codeThe 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:Copy codeThe 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;
}
}