Native JS Example

Source: Internet
Author: User

Using the native JS to make a class statistics

Implementation features:

1. Click Add to create a row of tables

2. Click Delete to delete a row of tables

3. Click on each cell to enter content, save content when focus is removed

Thinking Analysis:

1. Create a simple HTML table

2. Adding elements to the table via JS

(1): Add elements such as TR TD button

(2): Add Delete button

3. Adding content to a table through DOM operations

(1): Add a Click event to each TD

(2): Add tags to each TD

(3): Look for the tag and add the input tag to the TD labeled

(4): Focus Remove, delete tag, and re-add click event to each TD

(5): Gets the input value and assigns the value to the variable

(6): Enter the obtained variable into TD to Save

(7): Add Delete event to delete button, delete self tr

Code:

Create a simple HTML table

<body>
<center>
<table id= "tab" >
<tr>
<th width= "25%" > Grade </th>
<th width= "25%" > Sex </th>
<th width= "25%" > Name </th>
<th width= "25%" > Operations </th>
</tr>
</table>
<button id= "But" > Add </button>
</center>
</body>

Use JS to add content to it

Defining Global Variables

var tabdom= "";
var butdom= "";
var n=0;
var trdom= "";
var tddom= "";
var czdom= "";
var tdlength= "";
var dd= "";
var int= "";

Initialization
Window.onload = function () {
Tabdom=document.getelementbyid ("tab");
Butdom=document.getelementbyid ("but");
Butdom.addeventlistener ("click", Touch);
Tabdom.setattribute ("width", "50%");
Tabdom.setattribute ("Border", "1");
Dd=document.getelementsbytagname ("TD");
}

Define a method for adding a row
function Touch () {
n++; Set a tag to distinguish each TD
Trdom=document.createelement ("tr"); Using the DOM to add a TR
Trdom.style.height= "30px";
Tdlength=document.getelementsbytagname ("th"); Gets the number of times the th tag is used to determine TD label loops
for (Var i=0;i<tdlength.length;i++) {//Loop add TD and Delete button
Tddom=document.createelement ("TD"); Adding TD with Dom
if (i = = tdlength.length-1) {//Add a Delete button to the last TD
Czdom=document.createelement ("button"); Add a button using the DOM
Czdom.innerhtml= "Delete"; Add hint text to a button
Czdom.addeventlistener ("click", Del); Add a button Delete event
Czdom.setattribute ("Flage", N); Add markers to a button to differentiate each button
Tddom.appendchild (Czdom); Add a button to the TD
tddom.style.textalign= "center"; Set the properties of a button to distinguish between a cell with a button and a cell without a button
}
Trdom.appendchild (Tddom); TD added to the Recycle Bin TR
Trdom.setattribute ("Flage", N); Loop adds a tag to the TR
}
Tabdom.appendchild (Trdom); Loop to add TR to the table

Add a click event to each TD to add input


Dd=document.getelementsbytagname ("TD");

for (Var f=0;f<dd.length;f++) {
Alert (dd[f]);
Dd[f].addeventlistener ("click", add);
}
}
function Add () {//Add input Event
var td_a1= "";
var td_info=this; Ensure that this is the event itself does not change
if (td_info.style.textAlign! = "") {//Exclude Add input tag to button
Return
}
td_a1=td_info.innerhtml; Get the content in TD
Int=document.createelement ("input"); Add input Tag
Int.setattribute ("value", TD_A1); Add content to input
Int.addeventlistener ("Blur", Xiaoshi); Set the event for focus removal
Int.style.border= "None"; Delete border
Tddom. SetAttribute ("Set", "set"); Set the flag to differentiate TD
Td_info.appendchild (int); Add the input label to the TD
for (Var i=0;i<dd.length;i++) {//Find tagged TD Delete Click event
Dd[i].removeeventlistener ("click", add);
}
}
function Xiaoshi () {.//Set a method to cancel input
var td_a2=this; Ensure that this is the event itself does not change
var int_info=int.getattribute ("value");
if (Td_a2.getattribute ("set") = = "Set") {//Find marked TD
Td_a2.innerhtml=int_info; Give the content to TD
Td_a2.setattribute ("Set", ""); Clear marks
}

Add a click event to each TD


Dd=document.getelementsbytagname ("TD");
for (Var f=0;f<dd.length;f++) {
Dd[f].addeventlistener ("click", add);
}
}
Function del (obj) {//Definition Delete method
var obj1=this; Ensure that this is the event itself does not change
var tr=document.getelementsbytagname ("tr");
N=obj1.getattribute ("Flage"); Get tag
for (Var i=0;i<tr.length;i++) {//find tag and perform delete operation
if (N==tr[i].getattribute ("Flage")) {
Tr[i].remove ();
}
}
}

Easy to mistake Point Note:

1. Note that the Delete button and the TR tag, less when added will not find the tag and delete the table

2. Note that to obtain TD labels do not use Tddom,tddom is added, not the entire cell TD, should redefine TD

3. Note Set the label for TD

4. After deleting the Click event, you should re-add it to each TD

Native JS Example

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.