Home > Web Front-end > JS Tutorial > Refactoring Javascript code examples (comparison before and after refactoring)_javascript skills

Refactoring Javascript code examples (comparison before and after refactoring)_javascript skills

WBOY
Release: 2016-05-16 17:43:12
Original
1278 people have browsed it

Today I made several tutorial articles on asp.net combined with Javascript. Now let's look back at those Javascript scripts. Some of them are not ideal and too complicated. Now extract them and reconstruct them.
The previous one:

Copy the code 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 = "";
}
}

Javascript after reconstruction Script:
Copy code 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 two:
Copy code 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;
}
}
}

Refactored Javascript script:
Copy code 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;
}
}
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template