技巧
手册:http://www.w3school.com.cn/js/jsref_random.asp
1:js时间
function getTodayDate()
{
var year,day,month;
var today;
today=new Date();
year =today.getFullYear();
month = today.getMonth()+ 1;
day = today.getDate();
return year + "-" + month + "-" + day;
}
在Javascript中为String对象添加trim,ltrim,rtrim方法
利用Javascript中每个对象(Object)的prototype属性我们可以为Javascript中的内置对象添加我们自己的方法和属性。
\s*表示空格
String.prototype.Trim = function()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}
String.prototype.LTrim = function()
{
return this.replace(/(^\s*)/g, "");
}
String.prototype.Rtrim = function()
{
return this.replace(/(\s*$)/g, "");
}
<script language=javascript>
String.prototype.Trim = function()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}
var s = " leading and trailing spaces ";
window.alert(s + " (" + s.length + ")");
s = s.Trim();
window.alert(s + " (" + s.length + ")");
</script>
js按回车执行:
onkeypress="setfocus(this)"
function setfocus(e)
{
if(event.keyCode==13&&e.id=="user_name")
{
event.returnValue=false;
document.getElementById("user_pwd").focus();
}
else if(event.keyCode==13&&e.id=="user_pwd")
{
event.returnValue=false;
document.getElementById("btnLogin").click();
}
}
js操作dropdwonlist
a、指定选定值
function showList(val)
{
var schoolName=document.getElementById('schoolName');
var index=-1;
for(i=0;i<schoolName.length;i++)
{
if(schoolName.options[i].value==val)
{
index=i;break;
}
}
if(index!=-1)
{
schoolName.selectedIndex=index;
val=schoolName.options[index].value;
name=schoolName.options[index].name;
}
}
2、增加一个:List.options.add(new Option(val,name))
3、全部清空:List.options.length=0
4 、当前索引:List.selectedIndex
---------------------------------------------------------------------------------------
模式对话框:
window.showModalDialog(url,window,'dialogWidth:430px;dialogHeight:310px;status:no;help:no');
---------------------------------------------------------------------------------------
访问frame里面的东西:
document.frames["rightForm"].document.location.reload();
-----------------------------------------------------------------------------
不能通过“前进”和“后退”来访问已经被替换的URL,面每次都在服务端重新生成:
location.replace(URL)
---------------------------------------------------------------------------------------
<script type="text/javascript">
function addBookmark(title,url) {
if (window.sidebar) {
window.sidebar.addPanel(title, url,"");
} else if( document.all ) {
window.external.AddFavorite( url, title);
} else if( window.opera && window.print ) {
return true;
}
}
</script> 页面使用:<a href=javascript:addBookmark('我的网站','http://www.this.net/')>添加到收藏</a>
--------------------------------checkbox全选---------------------------------------------
<input id="selAll" type="checkbox" onclick="SelectAllCheckboxes(this)" />
function SelectAllCheckboxes(obj)
{
var theForm=obj.form;
var theChk=obj.checked;
for (var i=0;i<theForm.elements.length;i++)
{
var temp = theForm.elements[i];
if (temp.type=="checkbox" && temp.id!=obj.id && temp.checked!=theChk)
{
temp.click();//用click()事件,其他只读的不会受影响
}
}
}
*******************鼠标事件********************
<body onmousedown="a()">
<script>
function a()
{
switch(event.button)
{
case 1 : alert('left'); break;
case 2 : alert('right'); break;
case 3 : alert('left && right'); break;
case 4 : alert('middle'); break;
case 5 : alert('left && middle'); break;
case 6 : alert('right && middle'); break;
case 7 : alert('left && middle && right'); break;
}
}
</script>
-------------------------------------状态栏显示时间-----------------------------
<SCRIPT LANGUAGE="JavaScript">
<!--
function updateTime() {
tick = new Date();
hours = tick.getHours();
minutes = tick.getMinutes();
seconds = tick.getSeconds();
day = tick.getDay();
month = tick.getMonth();
date = tick.getDate();
year = tick.getYear();
current = "" + ((hours >12) ? hours -12 :hours)
current += ((minutes < 10) ? ":0" : ":") + minutes
current += ((seconds < 10) ? ":0" : ":") + seconds
current += (hours >= 12) ? " P.M." : " A.M."
if(day==0){var weekday = " 星期日"}
if(day==1){var weekday = " 星期一"}
if(day==2){var weekday = " 星期二"}
if(day==3){var weekday = " 星期三"}
if(day==4){var weekday = " 星期四"}
if(day==5){var weekday = " 星期五"}
if(day==6){var weekday = " 星期六"}
current +=(weekday)
window.status=current;
timerID = setTimeout("updateTime()",1000)
}
//-->
</SCRIPT>
<body onLoad="updateTime()">
</body>
------------------------------------------------------------
按回车转换tab 达到跳转下一个输入框效果
用客户端脚本在页面添加document的onkeydown事件,让页面在接受到回车事件后,进行Tab键的功能,即只要把event的keyCode由13变为9
<script language="javascript" for="document" event="onkeydown">
<!--
if(event.keyCode==13 && event.srcElement.type!='button' && event.srcElement.type!='submit' && event.srcElement.type!='reset' && event.srcElement.type!='textarea' && event.srcElement.type!='')
event.keyCode=9;
-->
</script>

浙公网安备 33010602011771号