Javascript table operation implements code _javascript techniques

Source: Internet
Author: User
<input id= "txtping"/> <table width= "border=" 0 "cellspacing=" 0 "cellpadding=" 1 "id=" ch "> <tr> & Lt;td> China Beijing </td> </tr> <tr> <td> China Shanghai </td> </tr> </table> <script language= "JavaScript" type= "Text/javascript" > var tabobj = document.getElementById ("ch"); Get the Table object for (var i=0; i<tabobj.rows.length;i++)//Add event to cell {tabobj.rows[i].onclick=function () {Document.getele Mentbyid ("txtping"). Value = This.innertext; Take value tabobj.rows[i].onmouseover=function () {this.style.background= "#0EF";}; Row discoloration Tabobj.rows[i].onmouseout=function () {this.style.background= "";}; tabobj.rows[i].style.cursor= "Pointer"; function sets (obj) {document.getElementById ("Div1"). Style.top = document.getElementById (obj). offsettop + DOCUMENT.G Etelementbyid (obj). offsetheight + "px"; document.getElementById ("Div1"). Style.left = document.getElementById (obj). offsetleft + Document.body.scrollLeft + " PX "; }//javascript Add Table function creattable () {///createelement function to create tbody,tr,td,textnode element//The meaning of the elements is not in this description var tables = Document.createeleme NT ("table"); Tables.setattribute ("id", "Chen"); Tables.setattribute ("Border", "1px"); Tables.setattribute ("width", "300px"); var tbody= document.createelement ("tbody"); var row=document.createelement ("tr"); var cell=document.createelement ("TD"); var textnode=document.createtextnode ("Test"); Because each element is the relationship between node and child node, the AppendChild () function is used to connect the nodes Cell.appendchild (textnode); Row.appendchild (cell); Tbody.appendchild (row); Tables.appendchild (TBODY); document.getElementById ("Div3"). appendchild (tables); Add a method for each cell (var j=0;j< tabobj.rows.length;j++)//Line {for (var h=0;h< tabobj.rows.item (0)) by adding a layer to ID number DIV3. ells.length;h++)//cell {tabobj.rows[j].cells[h].onclick=function () {ch (this);}; }} </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
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.