Javascript code _ javascript tips

Source: Internet
Author: User
This article introduces the implementation code for adding and copying rows in JS to delete rows. If you need it, refer to it. The Code is as follows:


Var customerInfoMethod = {
// Copy and Add rows
Copyrow: function (obj, id)
{
Var rowIndex = obj. parentNode. parentNode. rowIndex;
Var resrow = document. getElementById (id );
RowIndex --;
Var newRow = resrow. cloneNode (true); // document. createElement ("tr ");
Var parent = resrow. parentNode;
If (parent. lastChild = resrow ){
// If targetElement is the last child element of parent, insert newElement
Parent. appendChild (newRow );
} Else {
// If not, insert it to the front of the next sibling node of targetElement.
Parent. insertBefore (newRow, resrow. nextSibling );
}

// NewRow. innerHTML = resrow. innerHTML;
Var selectObj = newRow. getElementsByTagName ("select ");
For (var a = 0; SelectObj [a]. options [0]. selected = true;
}
Var inputObj = newRow. getElementsByTagName ("input ");
InputObj [0]. style. display = "";
InputObj [1]. style. display = "none ";
},
// Copy and Add rows
CopyrowUpdate: function (obj, id)
{
Var rowIndex = obj. parentNode. parentNode. rowIndex;
Var resrow = document. getElementById (id );
RowIndex --;
Var newRow = resrow. cloneNode (true); // document. createElement ("tr ");
Var parent = resrow. parentNode;
If (parent. lastChild = resrow ){
// If targetElement is the last child element of parent, insert newElement
Parent. appendChild (newRow );
} Else {
// If not, insert it to the front of the next sibling node of targetElement.
Parent. insertBefore (newRow, resrow. nextSibling );
}
// NewRow. innerHTML = resrow. innerHTML;
Var selectObj = newRow. getElementsByTagName ("select ");
For (var a = 0; SelectObj [a]. options [0]. selected = true;
}
Var inputObj = newRow. getElementsByTagName ("input ");
For (var I = 0; I InputObj [I]. value = "0 ";
}
InputObj [inputObj. length-2]. style. display = "";
InputObj [inputObj. length-1]. style. display = "none ";
},
// Delete a row
DeleteRow: function (rootId, obj ){
Var rootTable = $ (rootId );
RootTable. deleteRow (obj. parentNode. parentNode. rowIndex );
}
}

/**
Add line fanjf
*/
Function addRow (tableId, trId ){
Var tableObj = document. getElementById (tableId );
Var trObj = document. getElementById (trId );
// Var trIndex = trObj. rowIndex + 1;
Var rows = tableObj. rows. length;
Var cell = "";
Var tr = tableObj. insertRow (rows );
For (I = 0; I Cell = tableObj. rows (rows). insertCell (I );
Cell. innerText = trObj. cells (I). innerText;
Cell. className = trObj. cells (I). className;
Cell. innerHTML = trObj. cells (I). innerHTML;
}
}
/**
Delete line fanjf
*/
Function deleteRow (tableId, trId ){

Var tableObj = document. getElementById (tableId );
Var trObj = document. getElementById (trId );

// Var trIndex = trObj. rowIndex; // copy the row number of the tr.
Var rows = tableObj. rows. length;
If (rows> 0 ){
TableObj. deleteRow (rows-1 );
} Else {
Alert ("No deleted card number information! ");
}
}

Related Article

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.