Today there are a few asp.net with JavaScript tutorial articles. Now look at those JavaScript scripts that are not well written and overly complex. Now extract them and refactor them.
before a:
Copy Code code 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 = "";
}
}
JavaScript script after refactoring:
Copy Code code 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;": "";
}
before two:
Copy Code code 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;
}
}
}
JavaScript script after refactoring:
Copy Code code 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;
}
}