Full example of an editable table implemented by jQuery

  • 2021-06-29 06:19:32
  • OfStack

This article provides an example of an editable table implemented by jQuery.Share it for your reference, as follows:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <title> Editable table </title>
   <script type = "text/javascript" src="jquery-1.7.2.min.js"></script>
  <style type = "text/css">
   body{
    background:#c0c0c0;
   }
   #tab{
    border-collapse:collapse;
   }
   #tab td{
    width:50px;
    height:18px;
    border:1px solid;
    text-align:center;
   }
  </style>
  <script type = "text/javascript">
   $(function(){
    var tds = $("#tab tr td");
    editeTable(tds);
   });
   function editeTable(tds){
    tds.click(function(){
     var td=$(this);
     var oldText=td.text();
     var input=$("<input type='text' value='"+oldText+"'/>");
     td.html(input);
     input.click(function(){
      return false;
     });
     input.css("border-width","1px");
     input.css("font-size","12px");
     input.css("text-align","center");
     input.css("width","0px");
     input.width(td.width());
     input.trigger("focus").trigger("select");
     input.blur(function(){
      td.html(oldText);
     });
     input.keyup(function(event){
      var keyEvent=event || window.event;
      var key=keyEvent.keyCode;
      var input_blur=$(this);
      switch(key)
      {
       case 13:
        var newText=input_blur.val();
        td.html(newText);
        changeCurrConAttrByTable(currTableId);
       break;
       case 27:// Press esc Key, cancel the changes, make the text box text 
        td.html(oldText);
       break;
      }
     });
    });
   };
  </script>
 </head>
 <body>
  <table id = "tab">
   <tr>
    <td>1</td><td>1</td><td>1</td><td>1</td><td>1</td>
   </tr>
   <tr>
    <td>1</td><td>1</td><td>1</td><td>1</td><td>1</td>
   </tr>
   <tr>
    <td>1</td><td>1</td><td>1</td><td>1</td><td>1</td>
   </tr>
   <tr>
    <td>1</td><td>1</td><td>1</td><td>1</td><td>1</td>
   </tr>
   <tr>
    <td>1</td><td>1</td><td>1</td><td>1</td><td>1</td>
   </tr>
  </table>
 </body>
</html>

More readers interested in jQuery-related content can view this site's topics: Summary of jQuery Table (table) Operational Skills, Summary of jQuery Common Plug-ins and Usages, Summary of Ajax Usage in jquery, Summary of jQuery Drag-and-Drop Effects and Skills, Summary of jQuery Extension Skills, Summary of jQuery Common Classic Special Effects,jQuery Animation and Utility Summary and jquery Selector Usage Summary

I hope that the description in this paper will be helpful to everyone's jQuery program design.


Related articles: