1.文本框焦点问题
onBlur:当失去输入焦点后产生该事件
onFocus:当输入获得焦点后,产生该文件
Onchange:当文字值改变时,产生该事件
Onselect:当文字加亮后,产生该文件

<input type="text" value="郭强" onfocus="if(value=='郭强') {value=''}" onblur="if
(value=='') {value='郭强'}">点击时文字消失,失去焦点时文字再出现


2.网页按钮的特殊颜色
<input type=button name="Submit1" value="郭强" size=10 class="s02"

style="background-color:rgb(235,207,22)">

3.鼠标移入移出时颜色变化
<input type="submit" value="找吧" name="B1" onMouseOut=this.style.color="blue"

onMouseOver=this.style.color="red"  class="button">

4.平面按钮
<input type=submit value=订阅 style="border:1px solid :#666666; height:17px; width:25pt; font-size:9pt;

BACKGROUND-COLOR: #E8E8FF; color:#666666" name="submit">

5.按钮颜色变化
<input type=text name="nick"  style="border:1px solid #666666;  font-size:9pt;  height:17px;

BACKGROUND-COLOR: #F4F4FF; color:#ff6600" size="15" maxlength="16">

6.平面输入框
<input type="text" name="T1" size="20" style="border-style: solid; border-width: 1">

7.使窗口变成指定的大小
<script>
window.resizeTo(300,283);
</script>

8.使文字上下滚动
<marquee direction=up scrollamount=1 scrolldelay=100 onmouseover='this.stop()' onmouseout='this.start()'

height=60>
<!-- head_scrolltext -->
<tr>
<td>
共和国
</table>        <!-- end head_scrolltext -->
</marquee>

9.状态栏显示该页状态
<base onmouseover="window.status='网站建设 );
background-repeat: no-repeat;
}
.mouseout
{
background-color: #E0E0E0;
}
</style>

55.同时按下CTRL和Q键
document.onkeydown=function()
{
if(event.ctrlKey&&event.keyCode==81)
{alert(1)}
}//

56.以下是一个完整的显示hint的代码,其思想是当鼠标停留是将div中的内容显示在鼠标出,当鼠标移出后在将该div隐

藏掉

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<style>
#hint{
width:198px;
border:1px solid #000000;
background:#99ff33;
position:absolute;
z-index:9;
padding:6px;
line-height:17px;
text-align:left;
top: 1520px;
}
</style>
<SCRIPT LANGUAGE="JavaScript">
<!--
function showme()
{
var oSon=window.document.getElementById("hint");
if (oSon==null) return;
with (oSon)
{
  innerText=guoguo.value;
  style.display="block";
  style.pixelLeft=window.event.clientX+window.document.body.scrollLeft+6;
  style.pixelTop=window.event.clientY+window.document.body.scrollTop+9;
}
}
function hidme()
{
var oSon=window.document.getElementById("hint");
if (oSon==null) return;
oSon.style.display="none";
}
//-->
</SCRIPT>
<BODY>
<text id=guoguo value=ga>
<a href=# onmouseover=showme() onmouseout=hidme() onmousemove=showme() son=hint>dfdfd</a>
<div id=hint style="display:none"></div>
</BODY>
</HTML>
---------------------------------------------------------------------------------------------------------

------------

57.弹出窗口
方法一:<body onload="openwen()"> 浏览器读页面时弹出窗口;
方法二:<body onunload="openwen()"> 浏览器离开页面时弹出窗口;
方法三:用一个连接调用:<a href="#" onclick="openwin()">打开一个窗口</a>
注意:使用的"#"是虚连接。
方法四:用一个按钮调用:<input type="button" onclick="openwin()" value="打开窗口"> 何时装载script


58.动态改变字体的大小
function doZoom(size)
{
   document.getElementById('zoom').style.fontSize=size+'px'
}

function aa()
{
   var newWin=window.open(url);
   newWin.document.form1.text1.value=value1;
}改变弹出窗口上域的属性
opener.document.form2.text2.value=value2;改变父窗口的域的值

59.判断是何种浏览器
var name = navigator.appName;
if (name == "Microsoft Internet Explorer")
alert("IE");
else if (name == "Netscape")
alert("NS");//

60.vbsscript确定框
<script language="VBScript">
<!--
MsgBox "确定删除吗?", 4
//-->
</script>//

61.复制内容到剪切板
function JM_cc(bb)
{
    var ob=eval("document.form1."+bb);
    ob.select();
    js=ob.createTextRange();
    js.execCommand("Copy");
}//

62.java中建立数据库连接取数据
public void init()
{
String url="jdbc:odbc:javadata";
try
{
  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  Connection con=DriverManager.getConnection(url,"sa","");//mssql database user SA and password
  DatabaseMetaData dma=con.getMetaData();
  System.out.println("Connect to"+dma.getURL());
  System.out.println(";Driver "+dma.getDriverName());
  System.out.println(";Version "+dma.getDriverVersion());
  System.out.println("");
  Statement stmt=con.createStatement();
  ResultSet rs=stmt.executeQuery("select * from company.dbo.TB_NAME where number=1");//Sql
  rs.next();
  String dispresult=rs.getString("name");
  System.out.println(dispresult);// Instead,you can display it in Paint() or use AWT etc.
  rs.close();
  stmt.close();
  con.close();
}
catch(SQLException ex)
{
  System.out.println("!!!SQL Exception !!!");
  while(ex!=null)
  {
   System.out.println("SQLState:"+ex.getSQLState());
   System.out.println("Message:"+ex.getMessage());
   System.out.println("Vendor:"+ex.getErrorCode());
   ex=ex.getNextException();
   System.out.println("");
  }

}
catch(java.lang.Exception ex)
{
  ex.printStackTrace();
}
}//


63.最小化窗口
window.blur()//


64.文档的路径
document.URL//

65.定时执行某段程序
setTimeout("change_color()",600);

66.设置为主页
function makeHome(){
  netscape.security.PrivilegeManager.enablePrivilege("UniversalPreferencesWrite");
  navigator.preference("browser.startup.homepage", location.href);
}//

67.设置为收藏
function addFav(){
  if(ie)
    window.external.AddFavorite(location.href,'WWW.OGRISH.COM : GROTESQUE MOVIES AND PICTURES');
  if(ns)
    alert("Thanks for the bookmark!\n\nNetscape users click OK then press CTRL-D");
}//

68.判断cookie是否可用
navigator.cookieEnabled;//

69.显示有模式的有页面的弹出窗口
function setbgcolor_onclick()
{
var color = showModalDialog("/mailpage/compose/colorsel.html",0,"help=0");
if (color != null)
{
  document.compose.bgcolor.value = color;
}
}//


70.截取小数点后两位
var a=3454545.4454545;
alert(a.toFixed(2));//


71.禁止选择页面上的文字来拷贝
<script>
function noEffect() {
  with (event) {
    returnValue = false;
    cancelBubble = true;
  }
  return;
}
</script>
<body onselectstart="noEffect()" oncontextmenu="noEffect()">//

72.屏蔽右键菜单
oncontextmenu="event.returnValue = false"//

73.事件禁止起泡
event.cancelBubble = true//

74.禁止在输入框打开输入法
<input style="ime-mode: disabled">//

75.屏蔽汉字和空格
<input name="txt"><input type="submit" onClick="alert(!/[^ -}]|\s/.test(txt.value))">//

76.用javascript判断文件是否存在
function Exists(filespec)
{
if (filespec)
{
  var fso;
  fso = new ActiveXObject("Scripting.FileSystemObject");
  alert(fso.FileExists(filespec));
}
}
选择图片 <input type=file name=f1><p>
<input type="submit" onClick="Exists(f1.value)">//

77.获得当前的文本框选中的文字
<input onmouseup="alert(document.selection.createRange().text)" value=123>//

78.跳转至目标页面,同时不可返回
<a href="javascript:location.replace('http://www.sohu.com/')">sohu.com</a>//


79.获得当前的行是表格的第几行
<script>
function getrow(obj)
{
   if(event.srcElement.tagName=="TD"){
   curRow=event.srcElement.parentElement;
   alert("这是第"+(curRow.rowIndex+1)+"行");

   }
}
</script>

<table border="1" width="100%" onclick=getrow(this)>
  <tr>
    <td width="20%"> </td>
    <td width="20%"> </td>
    <td width="20%"> </td>
    <td width="20%"> </td>
    <td width="20%"> </td>
  </tr>
  <tr>
    <td width="20%"> </td>
    <td width="20%"> </td>
    <td width="20%"> </td>
    <td width="20%"> </td>
    <td width="20%"> </td>
  </tr>
</table>//

80.删除表格某行,xx表示某行,下标从0开始计算
document.all.myTable.deleteRow(xx)//

81.动态的向表格中添加行
<table ></textarea>//
 
132.判断是什么对象
if(typeof(unknown)=="function")return true;
if(typeof(unknown)!="object")return false;//


133.取消文本框自动完成功能
<input type="text" autocomplete="off"> //

134.让下拉框自动下拉
<select onmouseover="javascript:this.size=this.length" onmouseout="javascript:this.size=1">
<option value="">1</option>
<option value="">2</option>
<option value="">3</option>
</select> //


135.读取XML文件
var childrenobj=myselect//document.all.myselect;
    var oXMLDoc = new ActiveXObject('MSXML');
    oXMLDoc.url = "mymsg.xml";
    var oRoot=oXMLDoc.root;
    if(oRoot.children != null)
{
        for(var i=0;i<oRoot.children.item(0).children.length;++i)
  {
            oItem = oRoot.children.item(0).children.item(i);
            oOption = new Option(oItem.text,oItem.value);
   childrenobj.add(oOption);
        }
    }
//mymsg.xml文件
<?xml version="1.0" encoding="gb2312" ?>
<childrenlist>
<aa>
<child value='3301'>杭州地区</child>

<child value='3303'>温州地区</child>

</aa>
<aa>
<child value='3310'>台州地区</child>

<child value='3311'>丽水地区</child>
</aa>
</childrenlist>//


136.点击图片,图片停止
<a href="javascript:"><img src="http://www.51js.com/images/51js/red_forum.gif" border="0"></a>//

137.显示本地计算机信息
var WshNetwork = new ActiveXObject("WScript.Network");
alert("Domain = " + WshNetwork.UserDomain);
alert("Computer Name = " + WshNetwork.ComputerName);
alert("User Name = " + WshNetwork.UserName);//


138.比较时间
  tDate = new Date(2004,01,08,14,35); //年,月,日,时,分
  dDate = new Date();
  tDate<dDate?alert("早于"):alert("晚于");//

139.弹出鼠标所在处的链结地址
  <body onmouseover="if (event.srcElement.tagName=='A')alert(event.srcElement.href)"><a

href="http://51js.com/viewthread.php?tid=13589" >dddd</a><input>//

140.注意不能通过与 undefined 做比较来测试一个变量是否存在,虽然可以检查它的类型是否为“undefined”。在以

下的代码范例中,假设程序员想测试是否已经声明变量 x :
// 这种方法不起作用
if (x == undefined)
    // 作某些操作
// 这个方法同样不起作用- 必须检查


// 字符串 "undefined"
if (typeof(x) == undefined)
    // 作某些操作
// 这个方法有效
if (typeof(x) == "undefined")
    // 作某些操作

141.创建具有某些属性的对象
var myObject = new Object();
myObject.name = "James";
myObject.age = "22";
myObject.phone = "555 1234";//

142.枚举(循环)对象的所有属性
for (var a in myObject)
{
    // 显示 "The property 'name' is James",等等。
    window.alert("The property '" + a + "' is " + myObject[a]);
}//

143.判断一个数字是否是整数
var a=23.2;
alert(a%1==1)//


144.新建日期型变量
var a = new Date(2000, 1, 1);
alert(a.toLocaleDateString());


145.给类定义新的方法
function trim_1()
{
  return this.replace(/(^\s*)|(\s*$)/g, "");
}
String.prototype.trim=trim_1;
alert('cindy'.trim());


146.定义一个将日期类型转化为字符串的方法
function guoguo_date()
{
var tmp1,tmp2;
tmp1 =this.getMonth()+1+"";
if(tmp1.length<2)
  tmp1="0"+tmp1;
tmp2 =this.getDate()+"";
if(tmp2.length<2)
  tmp2="0"+tmp2;

return this.getYear()+"-"+tmp1+"-"+tmp2;
}
Date.prototype.toLiteString=guoguo_date;
alert(new Date().toLiteString())



147. pasta 是有四个参数的构造器,定义对象。
function pasta(grain, width, shape, hasEgg)
{
    // 是用什么粮食做的?
    this.grain = grain;

    // 多宽?(数值)
    this.width = width;    

    // 横截面形状?(字符串)
    this.shape = shape;  

    // 是否加蛋黄?(boolean)
    this.hasEgg = hasEgg;  

    //定义方法
    this.toString=aa;
}
function aa()
{
;
}
//定义了对象构造器后,用 new 运算符创建对象实例。
var spaghetti = new pasta("wheat", 0.2, "circle", true);
var linguine = new pasta("wheat", 0.3, "oval", true);
//补充定义属性,spaghetti和linguine都将自动获得新的属性
pasta.prototype.foodgroup = "carbohydrates";


148.打印出错误原因
try
{
x = y   // 产生错误。
}
catch(e)
{
   document.write(e.description)   //打印 "'y' is undefined".
}//

149.生成Excel文件并保存
var ExcelSheet;
ExcelApp = new ActiveXObject("Excel.Application");
ExcelSheet = new ActiveXObject("Excel.Sheet");
//本代码启动创建对象的应用程序(在这种情况下,Microsoft Excel 工作表)。一旦对象被创建,就可以用定义的对

象变量在代码中引用它。 在下面的例子中,通过对象变量 ExcelSheet 访问新对象的属性和方法和其他 Excel 对象,

包括 Application 对象和 ActiveSheet.Cells 集合。
// 使 Excel 通过 Application 对象可见。
ExcelSheet.Application.Visible = true;
// 将一些文本放置到表格的第一格中。
ExcelSheet.ActiveSheet.Cells(1,1).Value = "This is column A, row 1";
// 保存表格。
ExcelSheet.SaveAs("C:\\TEST.XLS");
// 用 Application 对象用 Quit 方法关闭 Excel。
ExcelSheet.Application.Quit();//

150.根据标签获得一组对象
var coll = document.all.tags("DIV");
if (coll!=null)
{
for (i=0; i<coll.length; i++)
...
}//
    
151.实现打印预览及打印
<OBJECT class)//读取页面上的XML数据岛中的数据
 
199.模式窗口
父窗口
var url="aaa.jsp";
var

data=showModalDialog(url,null,"dialogHeight:400px;dialogHeight:600px;center:yes;help:No;status:no;resizab

le:Yes;edge:sunken");
if(data)
alert(data.value);

子窗口
var data=new Object();
data.value1="china";
window.returnValue=data;
window.close();


200.动态设置事件,带参数
<INPUT TYPE="text" NAME="a1">
<SCRIPT LANGUAGE="JavaScript">
<!--
function hah(para)
{
alert(para)
}
a1.onclick=function()
{
hah('canshu ')
}
//a1.attachEvent("onclick",function(){hah('参数')});
//-->
</SCRIPT>//


201.将url转化为16进制形式
var ret = '';

for(var i=0; i < str.length; i++)
{
  var ch = str.charAt(i);
  var code = str.charCodeAt(i);

  if(code < 128 && ch != '[' && ch != '\'' && ch != '=')
  {
   ret += ch;
  }
  else
  {
   ret += "[" + code.toString(16) + "]";
  }
}
return ret;//


202.打开新的窗口并将新打开的窗口设置为活动窗口
var newWin=window.open("xxxx");
newWin.focus();//


203.容错脚本
JS中遇到脚本错误时不做任何操作:window.onerror = doNothing;
指定错误句柄的语法为:window.onerror = handleError
function handleError(message, URI, line)
{// 提示用户,该页可能不能正确回应
return true; // 这将终止默认信息
}//在页面出错时进行操作

204.JS中的窗口重定向:
window.navigate("http://www.sina.com.cn");//

205.防止链接文字折行
document.body.noWrap=true;//

206.判断字符是否匹配.
string.match(regExpression)//

207.
href="javascript:document.Form.Name.value='test';void(0);"//不能用onClick="javacript:document.Form.Name.v

alue='test';return false;"

当使用inline方式添加事件处理脚本事,有一个被包装成匿名函数的过程,也就是说
onClick="javacript:document.Form.Name.value='test';return false;"被包装成了:
functoin anonymous()
{
    document.Form.Name.value='test';return false;
}
做为A的成员函数onclick。
而href="javascript:document.Form.Name.value='test';void(0);"相当于执行全局语句,这时如果使用return语句会

报告在函数外使用return语句的错误。


208.进行页面放大
<P onmouseover="this.style.zoom='200%'" onmouseout="this.style.zoom='normal'">
sdsdsdsdsdsdsdsds
</p>//

209.放置在页面的最右边
<input type="text" value='bu2'  style="float:right">//

210.通过style来控制隔行显示不同颜色
<style>
tr{
bgcolor:expression(this.bgColor=((this.rowIndex)%2==0 )? 'white' : 'yellow');
}
</style>
<table >本页10秒后自动关闭,请注意刷新页面</p>

相关文章: