1、將字符轉(zhuǎn)換成數(shù)據(jù)
1)return str*1+1;
2)return parseInt(str);//return parseFloat(str);
3)new Number(str);
2、parentElement和parentNode區(qū)別
第一個(gè)是IE dom,第二個(gè)是標(biāo)準(zhǔn)DOM;換一個(gè)非ie瀏覽器試試就知道區(qū)別了(myie或maxthon或greenbrowser或tt之類都是ie瀏覽器……)
3、JS中的trim()——去前后的空格
String.prototype.trim=function(){return this.replace(/(^\s*)|(\s*$)/g,"");}
使用:" test str ".trim();返回結(jié)果:"test str";
4、報(bào)錯(cuò):missing ( before function parameters
不知道為什么,放到struts中會(huì)報(bào)這樣的錯(cuò),在其他工程下不會(huì)報(bào)錯(cuò),估計(jì)是JS格式不對(duì);
例如:把function String.prototype.trim(){return this.replace(/(^\s*)|(\s*$)/g,"");}修改為:
String.prototype.trim=function() {return this.replace(/(^\s*)|(\s*$)/g,"");}這樣就不會(huì)報(bào)錯(cuò)了。
5、window.showModalDialog不緩存
在Sturts的Action中加入:
response.setHeader("Pragma","No-Cache");
response.setHeader("Cache-Control","No-Cache");
response.setDateHeader("Expires", 0);
6、類似與Java中的startWith()和endWith()方法
String.prototype.endWith=function(str){
if(str==null||str==""||this.length==0||str.length>this.length)
return false;
if(this.substring(this.length-str.length)==str)
return true;
else
return false;
return true;
}
String.prototype.startWith=function(str){
if(str==null||str==""||this.length==0||str.length>this.length)
return false;
if(this.substr(0,str.length)==str)
return true;
else
return false;
return true;
}
應(yīng)用:"123456".startWith("123");-->結(jié)果為true;
應(yīng)用:"123456".endWith("456");-->結(jié)果為true;
7、限制錄入的分?jǐn)?shù)只能在(0~100之間),而且最多為一位小數(shù),且小數(shù)的數(shù)字只能為0或者5
String.prototype.limitDigit=function(){//限制最多為1位小數(shù)(返回值true/false)
var parttern=/^(\d+)(\.{0|5})?$/;
return parttern.test(this.replace(/(^\s*)|(\s*$)/g,""));
}
8、String有個(gè)屬性length,但是它不能區(qū)分英文字符,計(jì)算中文字符和全角字符。但是在數(shù)據(jù)存儲(chǔ)的時(shí)候中文和全角都是用兩個(gè)字節(jié)來(lái)存儲(chǔ)的,所有需要額外處理一下。自己寫了個(gè)函數(shù),返回String正真的長(zhǎng)度:
String.prototype.codeLength=function(){
var len=0;
if(this==null||this.length==0)
return 0;
var str=this.replace(/(^\s*)|(\s*$)/g,"");//去掉空格
for(i=0;i<str.length;i++)
if(str.charCodeAt(i)>0&&str.charCodeAt(i)<128)
len++;
else
len+=2;
return len;
}
9、徹底屏蔽鼠標(biāo)右鍵
<body oncontextmenu="window.event.returnValue=false">
或者:<body oncontextmenu="self.event.returnValue=false">
或者:<body oncontextmenu="return false">
10、防止剪切,復(fù)制,粘貼
<body oncopy="return false" oncut="return false" onpaste="return false">
11、關(guān)閉輸入法
<input type="text/password" style="ime-mode:disabled">
<textarea style="ime-mode:disabled"></textarea>
12、防止被人frame(盜鏈)
<script>
if(top.location!=self.location)
top.location=self.location;
</script>
13、網(wǎng)頁(yè)不能被另存為
<noscript><iframesrc=*.html></iframe></noscript>
14、按TAB鍵移動(dòng)到下一個(gè)輸入框時(shí),光標(biāo)停在文本框文字的最后,方便用戶繼續(xù)輸入
<script>
function moveEnd()
{
var e=event.srcElement;
var r=e.createTextRange();
r.moveStart('character',e.value.length);
r.collapse(true);
r.select();
}
</script>
<input type='text' value='0592' onfocus="moveEnd()">
15、判斷上一頁(yè)的來(lái)源(
我感覺無(wú)效)
document.referrer
16、最小化、最大化、關(guān)閉窗口(關(guān)閉窗口:無(wú)效)
<object id='winMin' classid="clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11">
<param name="Command" value="Minimize"></object>
<object id='winMax' classid="clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11">
<param name="Command" value="Maximize"></object>
<object id='winClo' classid="clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11">
<param name="Command" value="Close"></object>
<input type='button' value='最小化' onclick=winMin.Click()>
<input type='button' value='最大化' onclick=winMax.Click()>
<input type='button' value='關(guān)閉' onclick=winClo.Click()>
17、parseInt("8")和parseInt("08")得到的結(jié)果是不一樣的,前者得到"8"后者得到"0"。為什么?
語(yǔ)法:parseInt(numstring, [radix])
描述:numstring 必選項(xiàng)。要轉(zhuǎn)換為數(shù)字的字符串。radix 可選項(xiàng)。在 2 和 36 之間的表示 numstring 所保存數(shù)字的進(jìn)制的值。如果沒有提供,則前綴為 '0x' 的字符串被當(dāng)作十六進(jìn)制,前綴為 '0' 的字符串被當(dāng)作八進(jìn)制。所有其它字符串都被當(dāng)作是十進(jìn)制的。
現(xiàn)在再回過(guò)來(lái)看,parseInt("08")用的是八進(jìn)制,轉(zhuǎn)換出錯(cuò),所有得到"0"。
所以parseInt("8")和parseInt("08",10)得到的結(jié)果是一樣的。
18、限制只能輸入數(shù)字
只輸入數(shù)字:<input type="text" size="25" onkeyup="value=value.replace(/[^\d]/g,'')"/>
Email:onkeyup="value=value.replace(/[^\w|-|@|.]/g,'')"
19、onkeyup與onchange
onpropertychange能及時(shí)捕獲屬性值的變化
onkeyup事件,是在鍵盤按鍵松開時(shí)觸發(fā)的
onchange事件是在文本框獲得光標(biāo)前的值被改變(沒有改變時(shí)不會(huì)觸發(fā)),離開文本框時(shí)觸發(fā)的
20、將雙引號(hào)替換掉
例:限制輸入雙引號(hào)
<input type="text" onkeyup="this.value=this.value.replace(/\042/g,'')"/>
或者:
<input type="text" onkeyup="this.value=this.value.replace(/\x22/g,'')"/>
21、調(diào)試代碼(跟java的差不多)
try{
//……
}catch(err){
alert(err+err.description);
}
22、過(guò)濾HTML
在評(píng)論的時(shí)候?yàn)榱朔乐褂脩籼峤粠в袗阂獾哪_本,可以先過(guò)濾HTML標(biāo)簽,過(guò)濾掉雙引號(hào),單引號(hào),符號(hào)&,符號(hào)<,符號(hào)>。
String.prototype.filterHtml=function(){
return this.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""").replace(/'/g,"'");
}
23、readonly
在HTML中可以設(shè)置輸入框?qū)傩詒eadonly來(lái)控制不能輸入,但是如果在JS下需要注意,要寫成readOnly,注意大小寫(readOnly=true/false).
☆document.body.scrollTop的值始終為0
頁(yè)面直接用<html>開頭的話是沒有問(wèn)題的
使用DTD時(shí)用document.documentElement.scrollTop代替document.body.scrollTop就可以了。
posted on 2007-06-15 23:32
破繭而出 閱讀(2197)
評(píng)論(1) 編輯 收藏 所屬分類:
JavaScript