js技巧2

[html] view plain copy print ?
  1. 51.向文件中写内容 
  2. <%@ page import="java.io.*" %> 
  3. <
  4. String str = "print me"
  5. //always give the path from root. This way it almost always works. 
  6. String nameOfTextFile = "/usr/anil/imp.txt"
  7. try 
  8.   PrintWriter pw = new PrintWriter(new FileOutputStream(nameOfTextFile)); 
  9.   pw.println(str); 
  10.   //clean up 
  11.   pw.close(); 
  12. catch(IOException e) 
  13.   out.println(e.getMessage()); 
  14. %> 
  15.  
  16. 52.先读文件再写文件 
  17. <%@ page language = "java" %> 
  18. <%@ page contentType = "text/html; charSet=gb2312" %> 
  19. <%@ page import ="java.util.*" %> 
  20. <%@ page import ="java.lang.*" %> 
  21. <%@ page import ="javax.servlet.*" %> 
  22. <%@ page import ="javax.servlet.jsp.*" %> 
  23. <%@ page import ="javax.servlet.http.*" %> 
  24. <%@ page import="java.io.*" %> 
  25. eryrytry 
  26. <
  27. int count=0
  28. FileInputStream fi =new FileInputStream ("count.txt"); 
  29. ObjectInputStream si= new ObjectInputStream (fi); 
  30. count =si.readInt(); 
  31. count++; 
  32. out.print(count); 
  33. si.close(); 
  34.  
  35. FileOutputStream fo =new FileOutputStream ("count.txt"); 
  36. ObjectOutputStream so= new ObjectOutputStream (fo); 
  37. so.writeInt(count); 
  38. so.close(); 
  39. %> 
  40.  
  41. 53.直线型输入框 
  42. <INPUT name=Password size=10 type=password style="border-left-width: 0; border-right-width: 0; 
  43.  
  44. border-top-width: 0; border-bottom-style: solid; border-bottom-width: 1; background-color: #9CEB9C"> 
  45.  
  46. 54.可以将背景改为按钮性状,通过改变css改变属性 
  47. <td width="65" align="center" bgcolor="#E0E0E0" onmouseover=this.className='mouseoverbt'
  48.  
  49. onmouseout=this.className='mouseout';><a href="tm.asp?classid=76"><font 
  50.  
  51. color="#000000">录音笔</font></a></td> 
  52. <style> 
  53. .mouseoverbt 
  54. background-image: url(http://www.yongle.com.cn/img/btbgw64h20y.gif); 
  55. background-repeat: no-repeat; 
  56. .mouseout 
  57. background-color: #E0E0E0; 
  58. </style> 
  59.  
  60. 55.同时按下CTRL和Q键 
  61. document.onkeydown=function() 
  62. if(event.ctrlKey&&event.keyCode==81) 
  63. {alert(1)} 
  64. }// 
  65.  
  66. 56.以下是一个完整的显示hint的代码,其思想是当鼠标停留是将div中的内容显示在鼠标出,当鼠标移出后在将该div隐 
  67.  
  68. 藏掉 
  69. --------------------------------------------------------------------------------------------------------- 
  70.  
  71. ------------ 
  72. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
  73. <HTML> 
  74. <style> 
  75. #hint{ 
  76. width:198px; 
  77. border:1px solid #000000; 
  78. background:#99ff33; 
  79. position:absolute; 
  80. z-index:9; 
  81. padding:6px; 
  82. line-height:17px; 
  83. text-align:left; 
  84. top: 1520px; 
  85. </style> 
  86. <SCRIPT LANGUAGE="JavaScript"> 
  87. <!-- 
  88. function showme() 
  89. var oSon=window.document.getElementById("hint"); 
  90. if (oSon==null) return; 
  91. with (oSon) 
  92.   innerText=guoguo.value; 
  93.   style.display="block"
  94.   style.pixelLeft=window.event.clientX+window.document.body.scrollLeft+6; 
  95.   style.pixelTop=window.event.clientY+window.document.body.scrollTop+9; 
  96. function hidme() 
  97. var oSon=window.document.getElementById("hint"); 
  98. if (oSon==null) return; 
  99. oSon.style.display="none"
  100. //--> 
  101. </SCRIPT> 
  102. <BODY> 
  103. <text id=guoguo value=ga> 
  104. <a href=# onmouseover=showme() onmouseout=hidme() onmousemove=showme() son=hint>dfdfd</a> 
  105. <div id=hint style="display:none"></div> 
  106. </BODY> 
  107. </HTML> 
  108. --------------------------------------------------------------------------------------------------------- 
  109.  
  110. ------------ 
  111.  
  112. 57.弹出窗口 
  113. 方法一:<body onload="openwen()"> 浏览器读页面时弹出窗口; 
  114. 方法二:<body onunload="openwen()"> 浏览器离开页面时弹出窗口; 
  115. 方法三:用一个连接调用:<a href="#" onclick="openwin()">打开一个窗口</a> 
  116. 注意:使用的"#"是虚连接。 
  117. 方法四:用一个按钮调用:<input type="button" onclick="openwin()" value="打开窗口"> 何时装载script 
  118.  
  119.  
  120. 58.动态改变字体的大小 
  121. function doZoom(size) 
  122.    document.getElementById('zoom').style.fontSize=size+'px' 
  123.  
  124. function aa() 
  125.    var newWin=window.open(url); 
  126.    newWin.document.form1.text1.value=value1
  127. }改变弹出窗口上域的属性 
  128. opener.document.form2.text2.value=value2;改变父窗口的域的值 
  129.  
  130. 59.判断是何种浏览器 
  131. var name = navigator.appName; 
  132. if (name == "Microsoft Internet Explorer") 
  133. alert("IE"); 
  134. else if (name == "Netscape") 
  135. alert("NS");// 
  136.  
  137. 60.vbsscript确定框 
  138. <script language="VBScript"> 
  139. <!-- 
  140. MsgBox "确定删除吗?", 4 
  141. //--> 
  142. </script>// 
  143.  
  144. 61.复制内容到剪切板 
  145. function JM_cc(bb) 
  146.     var ob=eval("document.form1."+bb); 
  147.     ob.select(); 
  148.     js=ob.createTextRange(); 
  149.     js.execCommand("Copy"); 
  150. }// 
  151.  
  152. 62.java中建立数据库连接取数据 
  153. public void init() 
  154. String url="jdbc:odbc:javadata"
  155. try 
  156.   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
  157.   Connection con=DriverManager.getConnection(url,"sa","");//mssql database user SA and password 
  158.   DatabaseMetaData dma=con.getMetaData(); 
  159.   System.out.println("Connect to"+dma.getURL()); 
  160.   System.out.println(";Driver "+dma.getDriverName()); 
  161.   System.out.println(";Version "+dma.getDriverVersion()); 
  162.   System.out.println(""); 
  163.   Statement stmt=con.createStatement(); 
  164.   ResultSet rs=stmt.executeQuery("select * from company.dbo.TB_NAME where number=1");//Sql 
  165.   rs.next(); 
  166.   String dispresult=rs.getString("name"); 
  167.   System.out.println(dispresult);// Instead,you can display it in Paint() or use AWT etc. 
  168.   rs.close(); 
  169.   stmt.close(); 
  170.   con.close(); 
  171. catch(SQLException ex) 
  172.   System.out.println("!!!SQL Exception !!!"); 
  173.   while(ex!=null) 
  174.   { 
  175.    System.out.println("SQLState:"+ex.getSQLState()); 
  176.    System.out.println("Message:"+ex.getMessage()); 
  177.    System.out.println("Vendor:"+ex.getErrorCode()); 
  178.    ex=ex.getNextException(); 
  179.    System.out.println(""); 
  180.   } 
  181.  
  182. catch(java.lang.Exception ex) 
  183.   ex.printStackTrace(); 
  184. }// 
  185.  
  186.  
  187. 63.最小化窗口 
  188. window.blur()// 
  189.  
  190.  
  191. 64.文档的路径 
  192. document.URL// 
  193.  
  194. 65.定时执行某段程序 
  195. setTimeout("change_color()",600); 
  196.  
  197. 66.设置为主页 
  198. function makeHome(){ 
  199.   netscape.security.PrivilegeManager.enablePrivilege("UniversalPreferencesWrite"); 
  200.   navigator.preference("browser.startup.homepage", location.href); 
  201. }// 
  202.  
  203. 67.设置为收藏 
  204. function addFav(){ 
  205.   if(ie) 
  206.     window.external.AddFavorite(location.href,'WWW.OGRISH.COM : GROTESQUE MOVIES AND PICTURES'); 
  207.   if(ns) 
  208.     alert("Thanks for the bookmark!\n\nNetscape users click OK then press CTRL-D"); 
  209. }// 
  210.  
  211. 68.判断cookie是否可用 
  212. navigator.cookieEnabled;// 
  213.  
  214. 69.显示有模式的有页面的弹出窗口 
  215. function setbgcolor_onclick() 
  216. var color = showModalDialog("/mailpage/compose/colorsel.html",0,"help=0"); 
  217. if (color != null) 
  218.   document.compose.bgcolor.value = color
  219. }// 
  220.  
  221.  
  222. 70.截取小数点后两位 
  223. var a=3454545.4454545; 
  224. alert(a.toFixed(2));// 
  225.  
  226.  
  227. 71.禁止选择页面上的文字来拷贝 
  228. <script> 
  229. function noEffect() { 
  230.   with (event) { 
  231.     returnValue = false
  232.     cancelBubble = true
  233.   } 
  234.   return; 
  235. </script> 
  236. <body onselectstart="noEffect()" oncontextmenu="noEffect()">// 
  237.  
  238. 72.屏蔽右键菜单 
  239. oncontextmenu="event.returnValue = false"// 
  240.  
  241. 73.事件禁止起泡 
  242. event.cancelBubble = true// 
  243.  
  244. 74.禁止在输入框打开输入法 
  245. <input style="ime-mode: disabled">// 
  246.  
  247. 75.屏蔽汉字和空格 
  248. <input name="txt"><input type="submit" onClick="alert(!/[^ -}]|\s/.test(txt.value))">// 
  249.  
  250. 76.用javascript判断文件是否存在 
  251. function Exists(filespec) 
  252. if (filespec) 
  253.   var fso; 
  254.   fso = new ActiveXObject("Scripting.FileSystemObject"); 
  255.   alert(fso.FileExists(filespec)); 
  256. 选择图片 <input type=file name=f1><p> 
  257. <input type="submit" onClick="Exists(f1.value)">// 
  258.  
  259. 77.获得当前的文本框选中的文字 
  260. <input onmouseup="alert(document.selection.createRange().text)" value=123>// 
  261.  
  262. 78.跳转至目标页面,同时不可返回 
  263. <a href="javascript:location.replace('http://www.sohu.com/')">sohu.com</a>// 
  264.  
  265.   
  266. 79.获得当前的行是表格的第几行 
  267. <script> 
  268. function getrow(obj) 
  269.    if(event.srcElement.tagName=="TD"){ 
  270.    curRow=event.srcElement.parentElement; 
  271.    alert("这是第"+(curRow.rowIndex+1)+"行"); 
  272.  
  273.    } 
  274. </script> 
  275.  
  276. <table border="1" width="100%" onclick=getrow(this)> 
  277.   <tr> 
  278.     <td width="20%"> </td> 
  279.     <td width="20%"> </td> 
  280.     <td width="20%"> </td> 
  281.     <td width="20%"> </td> 
  282.     <td width="20%"> </td> 
  283.   </tr> 
  284.   <tr> 
  285.     <td width="20%"> </td> 
  286.     <td width="20%"> </td> 
  287.     <td width="20%"> </td> 
  288.     <td width="20%"> </td> 
  289.     <td width="20%"> </td> 
  290.   </tr> 
  291. </table>// 
  292.  
  293. 80.删除表格某行,xx表示某行,下标从0开始计算 
  294. document.all.myTable.deleteRow(xx)// 
  295.  
  296. 81.动态的向表格中添加行 
  297. <table id="t1" border="1"> 
  298. </table> 
  299. <script language="JavaScript"> 
  300. function add() 
  301.    t1.insertRow().insertCell().innerHTML = '<input name="test'+t1.rows.length+'">'; 
  302. }// 


你可能感兴趣的:(js技巧2)