JS实现即点即编辑功能

 

以前在网上都看到过类似的功能,不过没自己想要实现过,这次刚好做静态页面中有这样的一个需求,就试着自己做做看,做完发现也不是什么很难的事情。

效果如图所示:

 

分享代码喽:

  1. <html>
  2. <head>
  3. <title></title>
  4. <script LANGUAGE="JavaScript"> 
  5. <!--
  6. var globalLineId_1 = 1;
  7. var endLineId_1 = 5;
  8. var globalLineId_2 = 1;
  9. var endLineId_2 = 5;
  10. var globalLineId_3 = 1;
  11. var endLineId_3 = 5;
  12. var currTab = 1;
  13. function switchShow(isWrite,lineId, tabId){
  14.     var inputText = document.getElementById("inputText_"+tabId+"_"+lineId);
  15.     var inputDiv = document.getElementById("inputDiv_"+tabId+"_"+lineId);
  16.     var inputContentDiv = document.getElementById("inputContentDiv_"+tabId+"_"+lineId);
  17.     var inputContent = document.getElementById("inputContent");
  18.     var inputTd = document.getElementById("inputTd_"+tabId+"_"+lineId);
  19.     eval("globalLineId_"+tabId+"=lineId");
  20.     currTab = tabId;
  21.     if (isWrite){
  22.         inputText.value=inputDiv.innerText
  23.         inputDiv.style.display="none";
  24.         inputText.style.display="block";
  25.         inputContent.innerText = inputContentDiv.innerText;
  26.         inputText.focus();
  27.         if (eval("globalLineId_"+tabId) == eval("endLineId_"+tabId)){
  28.             addNewRow();
  29.         }
  30.     }else{
  31.         inputDiv.innerText=inputText.value;
  32.         inputDiv.style.display="block";
  33.         inputText.style.display="none";
  34.     }
  35. }
  36. function addNewRow(){
  37.     eval("endLineId_"+currTab+"++");
  38.     var currEndLineId = eval("endLineId_"+currTab);
  39.     var inputTab = document.getElementById("inputTab_" + currTab);
  40.     var newTr = inputTab.insertRow();
  41.     var newTd = newTr.insertCell();
  42.     newTd.id = "inputTd_"+currTab+"_"+currEndLineId;
  43.     newTd.style.height = "22px";
  44.     newTd.style.borderBottom = "solid 1px #000";
  45.     newTd.innerHTML = "<div id='inputDiv_"+currTab+"_"+currEndLineId+"' style='padding-left:3px;'> </div>" +
  46.     "<input id='inputText_"+currTab+"_"+currEndLineId+"' onblur='switchShow(false,"+currEndLineId+", "+currTab+");' type='text' class='inputmess' style='width:100%; display:none'>" +
  47.     "<div id='inputContentDiv_"+currTab+"_"+currEndLineId+"' style='display:none'></div>";
  48.     newTd.onclick = Function("switchShow(true, "+currEndLineId+", "+currTab+")");
  49. }
  50. function saveContent(){
  51.     var inputContentDiv = document.getElementById("inputContentDiv_"+currTab+"_"+eval("globalLineId_"+currTab));
  52.     var inputContent = document.getElementById("inputContent");
  53.     if(inputContentDiv !=null){
  54.         inputContentinputContentDiv.innerText = inputContent.innerText;
  55.     }
  56. }
  57. function deleteItem(){
  58.     var inputTd = document.getElementById("inputTd_"+currTab+"_"+eval("globalLineId_"+currTab));
  59.     var inputTab = document.getElementById("inputTab_"+currTab);
  60.     var inputRows = inputTab.rows;
  61.     var inputCells ;
  62.     var rowsLength = inputRows.length;
  63.     if (inputTd!=null){
  64.         for(i=1; i <rowsLength; i++){
  65.             inputCells = inputRows[i].getElementsByTagName("td");
  66.             if (inputCells[0].id=="inputTd_"+currTab+"_"+eval("globalLineId_"+currTab)){
  67.                 if (confirm("确认要删除吗?")){
  68.                     inputTab.deleteRow(inputRows[i].rowIndex);
  69.                     if (rowsLength-1<6){
  70.                         addNewRow();
  71.                     }
  72.                     return;
  73.                 }
  74.             }
  75.         }
  76.     }else{
  77.         alert("请选择再删除");
  78.     }
  79. }
  80. //--> 
  81. </script>
  82. </head>
  83. <body leftMargin="0" topMargin="1" marginwidth="0" marginheight="0"style="overflow: auto">
  84. <table width="100%" cellSpacing="0" cellPadding="0" border="0">
  85.     <tr height="30px">
  86.         <td></td>
  87.     </tr>
  88.     <tr valign="top" height="300px;">
  89.         <td width="50%" align="right">
  90.         <table cellSpacing="0" cellPadding="0" width="88%" align="center">
  91.             <tr>
  92.                 <td width="30%" align="center" valign="top">
  93.                 <div style="height: 130px; width: 100%; overflow-y: scroll; border: 1px solid #000;">
  94.                 <table id="inputTab_1" cellSpacing="0" cellPadding="0" width="100%" align="center" style="margin-top: 0px">
  95.                     <tr>
  96.                         <td style="padding-left: 5px; text-align: left; background-color: #EAEBDB; height: 25px; border-bottom: solid 1px #000">配置预算项目
  97.                         </td>
  98.                     </tr>
  99.                     <tr>
  100.                         <td id="inputTd_1_1" height="22px;" onclick="switchShow(true, 1, 1);" style="border-bottom: solid 1px #000;">
  101.                         <div id="inputDiv_1_1" style="padding-left: 3px;">教育学费</div>
  102.                         <input id="inputText_1_1" onblur="switchShow(false,1, 1);" type="text" class="inputmess" style="width: 100%; display: none" value="教育学费">
  103.                         <div id="inputContentDiv_1_1" style="display: none">大学学位已成为社会工作的基本门槛,也成为父母提供子女的基本,子女教育具有不具时间弹性和费用弹性,不象住房规划和退休规划,如财礼不足,可以减低标准,子女教育的时间点和费用比较固定,需要及早规划。</div>
  104.                         </td>
  105.                     </tr>
  106.                     <tr>
  107.                         <td id="inputTd_1_2" height="22px;" onclick="switchShow(true, 2, 1);" style="border-bottom: solid 1px #000;">
  108.                         <div id="inputDiv_1_2" style="padding-left: 3px;">住宿费</div>
  109.                         <input id="inputText_1_2" onblur="switchShow(false,2, 1);" type="text" class="inputmess" style="width: 100%; display: none" value="住宿费">
  110.                         <div id="inputContentDiv_1_2" style="display: none">住宿费项目说明。</div>
  111.                         </td>
  112.                     </tr>
  113.                     <tr>
  114.                         <td id="inputTd_1_3" height="22px;" onclick="switchShow(true, 3, 1);" style="border-bottom: solid 1px #000;">
  115.                         <div id="inputDiv_1_3" style="padding-left: 3px;">生活费</div>
  116.                         <input id="inputText_1_3" onblur="switchShow(false,3, 1);" type="text" class="inputmess" style="width: 100%; display: none" value="生活费">
  117.                         <div id="inputContentDiv_1_3" style="display: none">生活费项目说明。</div>
  118.                         </td>
  119.                     </tr>
  120.                     <tr>
  121.                         <td id="inputTd_1_4" height="22px;" onclick="switchShow(true, 4, 1);" style="border-bottom: solid 1px #000;">
  122.                         <div id="inputDiv_1_4" style="padding-left: 3px;">其他</div>
  123.                         <input id="inputText_1_4" onblur="switchShow(false,4, 1);" type="text" class="inputmess" style="width: 100%; display: none" value="其他">
  124.                         <div id="inputContentDiv_1_4" style="display: none">其他项目说明。</div>
  125.                         </td>
  126.                     </tr>
  127.                     <tr>
  128.                         <td id="inputTd_1_5" height="22px;" onclick="switchShow(true, 5, 1);" style="border-bottom: solid 1px #000;">
  129.                         <div id="inputDiv_1_5" style="padding-left: 3px;"></div>
  130.                         <input id="inputText_1_5" onblur="switchShow(false,5, 1);" type="text" class="inputmess" style="width: 100%; display: none" value="">
  131.                         <div id="inputContentDiv_1_5" style="display: none"></div>
  132.                         </td>
  133.                     </tr>
  134.                 </table>
  135.                 </div>
  136.                 <div style="height: 130px; width: 100%; overflow-y: scroll; border: 1px solid #000; margin-top: 10px">
  137.                 <table id="inputTab_2" cellSpacing="0" cellPadding="0" width="100%" align="center" style="margin-top: 0px">
  138.                     <tr>
  139.                         <td style="padding-left: 5px; text-align: left; background-color: #EAEBDB; height: 25px; border-bottom: solid 1px #000">配置适用地区
  140.                         </td>
  141.                     </tr>
  142.                     <tr>
  143.                         <td id="inputTd_2_1" height="22px;" onclick="switchShow(true, 1, 2);" style="border-bottom: solid 1px #000;">
  144.                         <div id="inputDiv_2_1" style="padding-left: 3px;">西北地区</div>
  145.                         <input id="inputText_2_1" onblur="switchShow(false,1, 2);" type="text" class="inputmess" style="width: 100%; display: none" value="西北地区">
  146.                         <div id="inputContentDiv_2_1" style="display: none">配置适用地区,西北地区,项目说明。</div>
  147.                         </td>
  148.                     </tr>
  149.                     <tr>
  150.                         <td id="inputTd_2_2" height="22px;" onclick="switchShow(true, 2, 2);" style="border-bottom: solid 1px #000;">
  151.                         <div id="inputDiv_2_2" style="padding-left: 3px;">福建省</div>
  152.                         <input id="inputText_2_2" onblur="switchShow(false,2, 2);" type="text" class="inputmess" style="width: 100%; display: none" value="福建省">
  153.                         <div id="inputContentDiv_2_2" style="display: none">配置适用地区,福建省,项目说明。</div>
  154.                         </td>
  155.                     </tr>
  156.                     <tr>
  157.                         <td id="inputTd_2_3" height="22px;" onclick="switchShow(true, 3, 2);" style="border-bottom: solid 1px #000;">
  158.                         <div id="inputDiv_2_3" style="padding-left: 3px;">北京市</div>
  159.                         <input id="inputText_2_3" onblur="switchShow(false,3, 2);" type="text" class="inputmess" style="width: 100%; display: none" value="北京市">
  160.                         <div id="inputContentDiv_2_3" style="display: none">配置适用地区,北京市,项目说明。</div>
  161.                         </td>
  162.                     </tr>
  163.                     <tr>
  164.                         <td id="inputTd_2_4" height="22px;" onclick="switchShow(true, 4, 2);" style="border-bottom: solid 1px #000;">
  165.                         <div id="inputDiv_2_4" style="padding-left: 3px;"></div>
  166.                         <input id="inputText_2_4" onblur="switchShow(false,4, 2);" type="text" class="inputmess" style="width: 100%; display: none" value="">
  167.                         <div id="inputContentDiv_2_4" style="display: none"></div>
  168.                         </td>
  169.                     </tr>
  170.                     <tr>
  171.                         <td id="inputTd_2_5" height="22px;" onclick="switchShow(true, 5, 2);" style="border-bottom: solid 1px #000;">
  172.                         <div id="inputDiv_2_5" style="padding-left: 3px;"></div>
  173.                         <input id="inputText_2_5" onblur="switchShow(false,5, 2);" type="text" class="inputmess" style="width: 100%; display: none" value="">
  174.                         <div id="inputContentDiv_2_5" style="display: none"></div>
  175.                         </td>
  176.                     </tr>
  177.                 </table>
  178.                 </div>
  179.                 <div style="height: 130px; width: 100%; overflow-y: scroll; border: 1px solid #000; margin-top: 10px">
  180.                 <table id="inputTab_3" cellSpacing="0" cellPadding="0" width="100%" align="center" style="margin-top: 0px">
  181.                     <tr>
  182.                         <td style="padding-left: 5px; text-align: left; background-color: #EAEBDB; height: 25px; border-bottom: solid 1px #000">配置预算方案
  183.                         </td>
  184.                     </tr>
  185.                     <tr>
  186.                         <td id="inputTd_3_1" height="22px;" onclick="switchShow(true, 1, 3);" style="border-bottom: solid 1px #000;">
  187.                         <div id="inputDiv_3_1" style="padding-left: 3px;">低成本预算方案</div>
  188.                         <input id="inputText_3_1" onblur="switchShow(false,1, 3);" type="text" class="inputmess" style="width: 100%; display: none" value="低成本预算方案">
  189.                         <div id="inputContentDiv_3_1" style="display: none">配置预算方案,低成本预算方案,项目说明。</div>
  190.                         </td>
  191.                     </tr>
  192.                     <tr>
  193.                         <td id="inputTd_3_2" height="22px;" onclick="switchShow(true, 2, 3);" style="border-bottom: solid 1px #000;">
  194.                         <div id="inputDiv_3_2" style="padding-left: 3px;">普通预算方案</div>
  195.                         <input id="inputText_3_2" onblur="switchShow(false, 2, 3);" type="text" class="inputmess" style="width: 100%; display: none" value="普通预算方案">
  196.                         <div id="inputContentDiv_3_2" style="display: none">配置预算方案,普通预算方案,项目说明。</div>
  197.                         </td>
  198.                     </tr>
  199.                     <tr>
  200.                         <td id="inputTd_3_3" height="22px;" onclick="switchShow(true, 3, 3);" style="border-bottom: solid 1px #000;">
  201.                         <div id="inputDiv_3_3" style="padding-left: 3px;">豪华预算方案</div>
  202.                         <input id="inputText_3_3" onblur="switchShow(false,3, 3);" type="text" class="inputmess" style="width: 100%; display: none" value="豪华预算方案">
  203.                         <div id="inputContentDiv_3_3" style="display: none">配置预算方案,豪华预算方案,项目说明。</div>
  204.                         </td>
  205.                     </tr>
  206.                     <tr>
  207.                         <td id="inputTd_3_4" height="22px;" onclick="switchShow(true, 4, 3);" style="border-bottom: solid 1px #000;">
  208.                         <div id="inputDiv_3_4" style="padding-left: 3px;"></div>
  209.                         <input id="inputText_3_4" onblur="switchShow(false,4, 3);" type="text" class="inputmess" style="width: 100%; display: none" value="">
  210.                         <div id="inputContentDiv_3_4" style="display: none"></div>
  211.                         </td>
  212.                     </tr>
  213.                     <tr>
  214.                         <td id="inputTd_3_5" height="22px;" onclick="switchShow(true, 5, 3);" style="border-bottom: solid 1px #000;">
  215.                         <div id="inputDiv_3_5" style="padding-left: 3px;"></div>
  216.                         <input id="inputText_3_5" onblur="switchShow(false, 5, 3);" type="text" class="inputmess" style="width: 100%; display: none" value="">
  217.                         <div id="inputContentDiv_3_5" style="display: none"></div>
  218.                         </td>
  219.                     </tr>
  220.                 </table>
  221.                 </div>
  222.                 </td>
  223.                 <td width="8%" align="center" valign="top"style="padding-right: 5px">
  224.                     <input type="button" value="删 除" onClick="deleteItem();"/>
  225.                 </td>
  226.                 <td width="50%" align="left" valign="top">
  227.                 <textarea id="inputContent" onblur="saveContent();" style="width: 90%; height: 410px">大学学位已成为社会工作的基本门槛,也成为父母提供子女的基本,子女教育具有不具时间弹性和费用弹性,不象住房规划和退休规划,如财礼不足,可以减低标准,子女教育的时间点和费用比较固定,需要及早规划。</textarea>
  228.                 </td>
  229.             </tr>
  230.         </table>
  231.         </td>
  232.     </tr>
  233. </table>
  234. </body>
  235. </html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值