JavaScript综合:画表格,合并单元格,右键菜单,模态窗口,气泡窗口

index.html

  
  
  
  
  1. <html> 
  2. <head> 
  3. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
  4. <title>表格操作</title> 
  5. <!--右键菜单--> 
  6. <script> 
  7.     //创建菜单  
  8.     function showDialog1(){      
  9.         omenu.style.left = -100;         
  10.         omenu.style.top = -100;      
  11.         var ijson = window.showModalDialog('创建.html',window,'dialogWidth:400px;dialogHeight:400px');         
  12.         var oimg = document.createElement("img");            
  13.         oimg.inum=ijson.inum;        
  14.         oimg.iname=ijson.iname;   
  15.         if(ijson.itype=="1"){     
  16.             oimg.itype="写字间";  
  17.             oimg.src="img/写字间.jpg";   
  18.         }  
  19.         else if(ijson.itype=="2"){     
  20.             oimg.itype="档口";  
  21.             oimg.src="img/档口.jpg";    
  22.         }  
  23.         else if(ijson.itype=="3"){     
  24.             oimg.itype="摊场";  
  25.             oimg.src="img/摊场.jpg";    
  26.         }        
  27.         oimg.imoney=ijson.imoney;        
  28.         oimg.title="编号:"+oimg.inum+"\n"+"名称:"+oimg.iname+"\n"+"类型:"+oimg.itype+"\n"+"租金:"+oimg.imoney+"\n";                 
  29.         objTD.appendChild(oimg);  
  30.         objTD.leftclick="false";  
  31.     }  
  32.     //编辑菜单  
  33.     function showDialog2(){  
  34.         omenu.style.left = -100;         
  35.         omenu.style.top = -100;  
  36.         var ijson = window.showModalDialog('编辑.html',objTD,'dialogWidth:400px;dialogHeight:400px');  
  37.         objTD.childNodes[0].inum=ijson.inum;  
  38.         objTD.childNodes[0].iname=ijson.iname;  
  39.         objTD.childNodes[0].itype=ijson.itype;  
  40.         objTD.childNodes[0].imoney=ijson.imoney;  
  41.         objTD.childNodes[0].title="编号:"+objTD.childNodes[0].inum+"\n"+"名称:"+objTD.childNodes[0].iname+"\n"+"类型:"+objTD.childNodes[0].itype+"\n"+"租金:"+objTD.childNodes[0].imoney+"\n";       
  42.     }  
  43.     //设置菜单  
  44.     function showDialog3(){  
  45.         omenu.style.left = -100;  
  46.         omenu.style.top = -100;  
  47.         var formula = window.showModalDialog('设置.html',window,'dialogWidth:400px;dialogHeight:400px');  
  48.         if(formula!=""){  
  49.             document.getElementById("formula").innerHTML="公式:"+formula;  
  50.             objTD.childNodes[0].imoney = (eval(formula.replace(/a/,objTD.childNodes[0].imoney)));  
  51.             objTD.childNodes[0].title="编号:"+objTD.childNodes[0].inum+"\n"+"名称:"+objTD.childNodes[0].iname+"\n"+"类型:"+objTD.childNodes[0].itype+"\n"+"租金:"+objTD.childNodes[0].imoney+"\n";  
  52.         }  
  53.     }  
  54.     //删除菜单  
  55.     function del(){  
  56.         objTD.removeChild(objTD.childNodes[0]);  
  57.         objTD.rightclick="true";  
  58.         objTD.leftclick="true";  
  59.         objTD.style.backgroundColor="";  
  60.     }  
  61.     //租用菜单  
  62.     function rent(){  
  63.         omenu.style.left = -100;  
  64.         omenu.style.top = -100;  
  65.         objTD.style.backgroundColor='#999999';  
  66.         objTD.leftclick="false";  
  67.     }  
  68.  
  69.     //画表格          
  70.     function draw(){  
  71.         if(document.getElementById("rowNum").value==""){  
  72.                 alert("行非空!");  
  73.             return false;  
  74.         }  
  75.         else if(isNaN(document.getElementById("rowNum").value)){  
  76.             alert("行非字符!");  
  77.                 return false;  
  78.         }  
  79.         else if(document.getElementById("colNum").value==""){  
  80.                 alert("列非空!");  
  81.                 return false;  
  82.         }  
  83.         else if(isNaN(document.getElementById("colNum").value)){  
  84.                 alert("列非字符!");  
  85.                 return false;  
  86.         }  
  87.         var row=document.getElementById("rowNum").value;  
  88.         var col=document.getElementById("colNum").value;  
  89.         s="<table id='table1' border='1' cellpadding='0' cellspacing='0' ";  
  90.         s+="style='border-collapse:collapse' oncontextmenu=window.event.returnValue=false>";  
  91.         for(var i=0;i<row;i++){  
  92.             ss=s+"<tr>";  
  93.             for(var j=0;j<col;j++){  
  94.                 ss=s+"<td onclick='fun_onclick(this);' onmousedown='showMenu(this);'style='width:50px; height:50px' rightclick='true' leftclick='true'></td>";  
  95.             }  
  96.             ss=s+"</tr>";  
  97.          }  
  98.          ss=s+"</table>";  
  99.          tables.innerHTML=s;  
  100.     }  
  101.  
  102.     var x1;  
  103.     var x2;  
  104.     var first = true;  
  105.     var objTD;   
  106.     var omenu;  
  107.     //单击选择单元格事件  
  108.     function fun_  
  109.         if(obj.leftclick=="true"){  
  110.             if(first){  
  111.                 first = false;  
  112.                 x1=obj;  
  113.             }  
  114.             else{  
  115.                 first = true;  
  116.                 x2=obj;  
  117.             }         
  118.             obj.style.backgroundColor='#0000ff';  
  119.             obj.rightclick = "false";  
  120.         }  
  121.     }  
  122.  
  123.     function rebulid(){   
  124.         //设置要合并的单元格边框  
  125.         x1.style.border="0";  
  126.         x2.style.border="0";         
  127.     }  
  128.       
  129.     //右键单元格  
  130.     function  showMenu(obj){          
  131.         omenu = document.getElementById("main_menu");  
  132.         if(window.event.button == 2 && obj.rightclick=="true"){   
  133.             objobjTD = obj;      
  134.             omenu.style.left = event.x;  
  135.             omenu.style.top = event.y;  
  136.         }  
  137.         else{  
  138.             omenu.style.left = -100;  
  139.             omenu.style.top = -100;  
  140.         }  
  141.     }  
  142.       
  143. </script> 
  144. </head> 
  145. <body> 
  146.  
  147. <div > 
  148. 行:<input id="rowNum" type="text"/> 
  149. 列:<input id="colNum" type="text"/> 
  150. <input id="Button1" type="button" value="绘制表格"  onclick="draw()" /> 
  151. <input id="Button2" type="button" value="合并单元格"  onclick="rebulid()" /> 
  152. </div> 
  153. <div id="formula" ></div> 
  154. <br/> 
  155.  
  156. <span id="tables"></span> 
  157. <div id="main_menu"   
  158. style="position:absolute; background-color:#999999; left:-100px; top:-100px; width:80px; height:200px;"> 
  159. <div onClick="showDialog1()">&nbsp;创建</div> 
  160. <br /> 
  161. <div onClick="showDialog2()">&nbsp;编辑</div> 
  162. <br /> 
  163. <div onClick="del()">&nbsp;删除</div> 
  164. <br /> 
  165. <div  onClick="showDialog3()">&nbsp;设置</div> 
  166. <br /> 
  167. <div onClick="rent()">&nbsp;租用</div> 
  168. </div> 
  169. <div> 
  170. <img title="123" style="position:absolute;left:-100px; top:-100px"/> 
  171. </div> 
  172. </body> 
  173. </html> 

创建.html

  
  
  
  
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  2. <html xmlns="http://www.w3.org/1999/xhtml"> 
  3. <head> 
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
  5. <title>右键菜单创建</title> 
  6. <script language="javascript" type="text/javascript"> 
  7.     function fun1()  
  8.     {  
  9.         var ijson = {};  
  10.         ijson.inum = document.form1.inum.value;  
  11.         ijson.iname = document.form1.iname.value;  
  12.         ijson.itype = document.getElementById("itype").value;  
  13.         ijson.imoney = document.form1.imoney.value;  
  14.         parent.window.returnValue=ijson; //父窗口返回值  
  15.         window.close();  
  16.     }  
  17. </script> 
  18. </head> 
  19.  
  20. <body> 
  21. <form name="form1" action="" > 
  22. 编号:<input type="text" name="inum"/><br /> 
  23. 名称:<input type="text" name="iname"/><br /> 
  24. 类型:<select id="itype" > 
  25. <option value="0" >---请选择---</option> 
  26. <option value="1" >写字间</option> 
  27. <option value="2" >档口</option> 
  28. <option value="3" >摊场</option> 
  29. </select><br /> 
  30. 租金:<input type="text" name="imoney"/><br /> 
  31. <input id="Button1" type="button" value="提交" onclick="fun1();"/> 
  32. <input id="Button2" type="button" value="关闭" onclick="window.close();" /> 
  33. </form> 
  34. </body> 
  35. </html> 

编辑.html

  
  
  
  
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  2. <html xmlns="http://www.w3.org/1999/xhtml"> 
  3. <head> 
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
  5. <title>右键菜单编辑</title> 
  6. <script> 
  7. window.onload=function(){  
  8.     var txts = document.getElementsByTagName("input");  
  9.     txts[0].value=window.dialogArguments.childNodes[0].inum;  
  10.     txts[1].value=window.dialogArguments.childNodes[0].iname;  
  11.     txts[2].value=window.dialogArguments.childNodes[0].itype;  
  12.     txts[3].value=window.dialogArguments.childNodes[0].imoney;  
  13. }  
  14. function fun1(){  
  15.     var ijson = {};  
  16.     ijson.inum = document.form1.inum.value;  
  17.     ijson.iname = document.form1.iname.value;  
  18.     ijson.itype = document.form1.itype.value;  
  19.     ijson.imoney = document.form1.imoney.value;  
  20.     parent.window.returnValue=ijson; //父窗口返回值  
  21.     window.close();  
  22. }  
  23. </script> 
  24. </head> 
  25.  
  26. <body> 
  27. <form name="form1" action="" > 
  28. 编号:<input name="inum"   type="text"  readonly="true" onclick="alert('编号不能修改');"/><br /> 
  29. 名称:<input name="iname"  type="text"  /><br /> 
  30. 类型:<input name="itype"  type="text"  /><br /> 
  31. 租金:<input name="imoney" type="text"  /><br /> 
  32. <input id="Button1" type="button" value="提交" onclick="fun1();"/> 
  33. <input id="Button2" type="button" value="关闭" onclick="window.close();" /> 
  34. </form> 
  35. </body> 
  36. </html> 

设置.html

  
  
  
  
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  2. <html xmlns="http://www.w3.org/1999/xhtml"> 
  3. <head> 
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
  5. <title>右键菜单设置</title> 
  6. <script language="javascript" type="text/javascript"> 
  7.     function fun1()  
  8.     {  
  9.         var formula = document.form1.formula.value;  
  10.         parent.window.returnValue=formula; //父窗口返回值  
  11.        window.close();  
  12.     }  
  13. </script> 
  14. </head> 
  15.  
  16. <body> 
  17. <form name="form1" action="">   
  18. 请输入公式:<input type="text" name="formula"><br /> 
  19. <input id="Button1" type="button" value="提交" onclick="fun1();" /> 
  20. <input id="Button2" type="button" value="关闭" onclick="window.close();" /> 
  21. </form> 
  22. </body> 
  23. </html> 

 

你可能感兴趣的:(合并单元格,右键菜单,模态窗口,画表格,气泡窗口)