一、JavaScript技巧收集(231)
1.文本框焦点问题
onBlur:当失去输入焦点后产生该事件
onFocus:当输入获得焦点后,产生该文件
Onchange:当文字值改变时,产生该事件
Onselect:当文字加亮后,产生该文件
<input type="text" value="a" >点击时文字消失,失去焦点时文字再出现
2.网页按钮的特殊颜色
<input type=button name="Submit1" value="a" 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
height=60>
<!-- head_scrolltext -->
<tr>
<td>
共和国
</table> <!-- end head_scrolltext -->
</marquee>
9.状态栏显示该页状态
<base >
10.可以点击文字实现radio选项的选定
<br>
<input type="radio" name="regtype" value="A03" );
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 >//
72.屏蔽右键菜单
//
73.事件禁止起泡
event.cancelBubble = true//
74.禁止在输入框打开输入法
<input style="ime-mode: disabled">//
75.屏蔽汉字和空格
<input name="txt"><input type="submit" >//
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" >//
77.获得当前的文本框选中的文字
<input value=123>//
78.跳转至目标页面,同时不可返回
<a href="javascript:location.replace(http://www.upschool.com.cn'/)">教程中国</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 >//
119.添加节点
addNode()//
120.获得事件的父与子标签
event.srcElement.children[0]和event.srcElement.parentElement //
121.集中为按钮改变颜色
<style>
button{}{benc:expression(this.onfocus = function(){this.style.backgroundColor='#E5F0FF';})}
</style>
<button>New</button>//
122.判断是左键还是右键被按下
<body onmousedown=if(event.button==1)alert("左键");if(event.button==2)alert("右键")>//
123.获得操作系统的名称和浏览器的名称
document.write(navigator.userAgent)//
124.alt/ctrl/shift键按下
event.altKey //按下alt键
event.ctrlKey //按下ctrl键
event.shiftKey //按下shift键
125.将当前位置定位为C盘。
{window.location="c:"}//
126.返回输入框的类型
<script>
alert(event.srcElement.type);//
</script>
127.模拟控件的单击事件
<INPUT TYPE="hidden" name="guoguo" >
<SCRIPT LANGUAGE="JavaScript">
<!--
function haha()
{
alert();
}
guoguo.click();
//-->
</SCRIPT>//
128.取出记录集的列名
java.sql.ResultSet rset = com.bsitc.util.DBAssist.getIT().executeQuery(queryStatement, conn);
java.sql.ResultSetMetaData metaData = rset.getMetaData();
int count = metaData.getColumnCount();
String name = metaData.getColumnName(i);
String value = rset.getString(i);//
129.格式化数字
function format_number(str,digit)
{
if(isNaN(str))
{
alert("您传入的值不是数字!");
return 0;
}
else if(Math.round(digit)!=digit)
{
alert("您输入的小数位数不是整数!");
return 0;
}
else
return Math.round(parseFloat(str)*Math.pow(10,digit))/Math.pow(10,digit);
}
130.回车按钮转化为tab按钮
if(event.keyCode==13) event.keyCode=9; //将
131.滚动条滚动
<button >Scroll</button><br>
<textarea ,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.upschool.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>