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

可莉
2021-10-11
1381 0 0
点赞
收藏
评论区
加载中...