javascript
JavaScript 常用函数
javascript函數(shù)一共可分為五類:?
·常規(guī)函數(shù)?
·數(shù)組函數(shù)?
·日期函數(shù)?
·數(shù)學(xué)函數(shù)?
·字符串函數(shù)?
1.常規(guī)函數(shù)?
javascript常規(guī)函數(shù)包括以下9個(gè)函數(shù):?
(1)alert函數(shù):顯示一個(gè)警告對話框,包括一個(gè)OK按鈕。?
(2)confirm函數(shù):顯示一個(gè)確認(rèn)對話框,包括OK、Cancel按鈕。?
(3)escape函數(shù):將字符轉(zhuǎn)換成Unicode碼。?
(4)eval函數(shù):計(jì)算表達(dá)式的結(jié)果。?
(5)isNaN函數(shù):測試是(true)否(false)不是一個(gè)數(shù)字。?
(6)parseFloat函數(shù):將字符串轉(zhuǎn)換成符點(diǎn)數(shù)字形式。?
(7)parseInt函數(shù):將符串轉(zhuǎn)換成整數(shù)數(shù)字形式(可指定幾進(jìn)制)。?
(8)prompt函數(shù):顯示一個(gè)輸入對話框,提示等待用戶輸入。例如:?
<script language="javascript">?
<!--?
alert("輸入錯(cuò)誤");?
prompt("請輸入您的姓名","姓名");?
confirm("確定否!");?
//-->?
script>?
(9)unescape函數(shù):解碼由escape函數(shù)編碼的字符。?
2.數(shù)組函數(shù)?
javascript數(shù)組函數(shù)包括以下4個(gè)函數(shù):?
(1)join函數(shù):轉(zhuǎn)換并連接數(shù)組中的所有元素為一個(gè)字符串。例:?
function JoinDemo()?
{?
var a, b;?
a = new Array(0,1,2,3,4);?
b = a.join("-");//分隔符?
return(b);//返回的b=="0-1-2-3-4"?
}?
(2)length函數(shù):返回?cái)?shù)組的長度。例:?
function LengthDemo()?
{?
var a, l;?
a = new Array(0,1,2,3,4);?
l = a.length;?
return(l);//l==5?
}?
(3)reverse函數(shù):將數(shù)組元素順序顛倒。例:?
function ReverseDemo()?
{?
var a, l;?
a = new Array(0,1,2,3,4);?
l = a.reverse();?
return(l);?
}?
(4)sort函數(shù):將數(shù)組元素重新排序。例:?
function SortDemo()?
{?
var a, l;?
a = new Array("X" ,"y" ,"d", "Z", "v","m","r");?
l = a.sort();?
return(l);?
}?
3.日期函數(shù)?
javascript日期函數(shù)包括以下20個(gè)函數(shù):?
(1)getDate函數(shù):返回日期的"日"部分,值為1~31。例:?
function DateDemo()?
{?
var d, s = "Today's date is: ";?
d = new Date();?
s += (d.getMonth() + 1) + "/";?
s += d.getDate() + "/";?
s += d.getYear();?
return(s);?
}?
(2)getDay函數(shù):返回星期幾,值為0~6,其中0表示星期日,1表示星期一,...,6表示星期六。例:?
function DateDemo()?
{?
var d, day, x, s = "Today is: ";?
var x = new Array("Sunday", "Monday", "Tuesday");?
var x = x.concat("Wednesday","Thursday", "Friday");?
var x = x.concat("Saturday");?
d = new Date();?
day = d.getDay();?
return(s += x[day]);?
}?
(3)getHouse函數(shù):返回日期的"小時(shí)"部分,值為0~23。例。?
function TimeDemo()?
{?
var d, s = "The current local time is: ";?
var c = ":";?
d = new Date();?
s += d.getHours() + c;?
s += d.getMinutes() + c;?
s += d.getSeconds() + c;?
s += d.getMilliseconds();?
return(s);?
}?
(4)getMinutes函數(shù):返回日期的"分鐘"部分,值為0~59。見上例。?
(5)getMonth函數(shù):返回日期的"月"部分,值為0~11。其中0表示1月,2表示3月,...,11表示12月。見前面的例子。?
(6)getSeconds函數(shù):返回日期的"秒"部分,值為0~59。見前面的例子。?
(7)getTime函數(shù):返回系統(tǒng)時(shí)間。?
function GetTimeTest()?
{?
var d, s, t;?
var MinMilli = 1000 * 60;?
var HrMilli = MinMilli * 60;?
var DyMilli = HrMilli * 24;?
d = new Date();?
t = d.getTime();?
s = "It's been "?
s += Math.round(t / DyMilli) + " days since 1/1/70";?
return(s);?
}?
(8)getTimezoneOffset函數(shù):返回此地區(qū)的時(shí)差(當(dāng)?shù)貢r(shí)間與GMT格林威治標(biāo)準(zhǔn)時(shí)間的地區(qū)時(shí)差),單位為分鐘。?
function TZDemo()?
{?
var d, tz, s = "The current local time is ";?
d = new Date();?
tz = d.getTimezoneOffset();?
if (tz < 0)?
s += tz / 60 + " hours before GMT";?
else if (tz == 0)?
s += "GMT";?
else?
s += tz / 60 + " hours after GMT";?
return(s);?
}?
(9)getYear函數(shù):返回日期的"年"部分。返回值以1900年為基數(shù),例如1999年為99。前面有例子。?
(10)parse函數(shù):返回從1970年1月1日零時(shí)整算起的毫秒數(shù)(當(dāng)?shù)貢r(shí)間)。?
function GetTimeTest(testdate)?
{?
var d, s, t;?
var MinMilli = 1000 * 60;?
var HrMilli = MinMilli * 60;?
var DyMilli = HrMilli * 24;?
d = new Date();?
t = Date.parse(testdate);?
s = "There are "?
s += Math.round(Math.abs(t / DyMilli)) + " days "?
s += "between " + testdate + " and 1/1/70";?
return(s);?
}?
(11)setDate函數(shù):設(shè)定日期的"日"部分,值為0~31。?
(12)setHours函數(shù):設(shè)定日期的"小時(shí)"部分,值為0~23。?
(13)setMinutes函數(shù):設(shè)定日期的"分鐘"部分,值為0~59。?
(14)setMonth函數(shù):設(shè)定日期的"月"部分,值為0~11。其中0表示1月,...,11表示12月。?
(15)setSeconds函數(shù):設(shè)定日期的"秒"部分,值為0~59。?
(16)setTime函數(shù):設(shè)定時(shí)間。時(shí)間數(shù)值為1970年1月1日零時(shí)整算起的毫秒數(shù)。?
(17)setYear函數(shù):設(shè)定日期的"年"部分。?
(18)toGMTString函數(shù):轉(zhuǎn)換日期成為字符串,為GMT格林威治標(biāo)準(zhǔn)時(shí)間。?
(19)setLocaleString函數(shù):轉(zhuǎn)換日期成為字符串,為當(dāng)?shù)貢r(shí)間。?
(20)UTC函數(shù):返回從1970年1月1日零時(shí)整算起的毫秒數(shù),以GMT格林威治標(biāo)準(zhǔn)時(shí)間計(jì)算。?
4.數(shù)學(xué)函數(shù)?
javascript數(shù)學(xué)函數(shù)其實(shí)就是Math對象,它包括屬性和函數(shù)(或稱方法)兩部分。其中,屬性主要有下列內(nèi)容。?
Math.e:e(自然對數(shù))、Math.LN2(2的自然對數(shù))、Math.LN10(10的自然對數(shù))、Math.LOG2E(e的對數(shù),底數(shù)為2)、???
????? Math.LOG10E(e的對數(shù),底數(shù)為10)、Math.PI(π)、Math.SQRT1_2(1/2的平方根值)、Math.SQRT2(2的平方根值)。?
函數(shù)有以下18個(gè):?
(1)abs函數(shù):即Math.abs(以下同),返回一個(gè)數(shù)字的絕對值。?
(2)acos函數(shù):返回一個(gè)數(shù)字的反余弦值,結(jié)果為0~π弧度(radians)。?
(3)asin函數(shù):返回一個(gè)數(shù)字的反正弦值,結(jié)果為-π/2~π/2弧度。?
(4)atan函數(shù):返回一個(gè)數(shù)字的反正切值,結(jié)果為-π/2~π/2弧度。?
(5)atan2函數(shù):返回一個(gè)坐標(biāo)的極坐標(biāo)角度值。?
(6)ceil函數(shù):返回一個(gè)數(shù)字的最小整數(shù)值(大于或等于)。?
(7)cos函數(shù):返回一個(gè)數(shù)字的余弦值,結(jié)果為-1~1。?
(8)exp函數(shù):返回e(自然對數(shù))的乘方值。?
(9)floor函數(shù):返回一個(gè)數(shù)字的最大整數(shù)值(小于或等于)。?
(10)log函數(shù):自然對數(shù)函數(shù),返回一個(gè)數(shù)字的自然對數(shù)(e)值。?
(11)max函數(shù):返回兩個(gè)數(shù)的最大值。?
(12)min函數(shù):返回兩個(gè)數(shù)的最小值。?
(13)pow函數(shù):返回一個(gè)數(shù)字的乘方值。?
(14)random函數(shù):返回一個(gè)0~1的隨機(jī)數(shù)值。?
(15)round函數(shù):返回一個(gè)數(shù)字的四舍五入值,類型是整數(shù)。?
(16)sin函數(shù):返回一個(gè)數(shù)字的正弦值,結(jié)果為-1~1。?
(17)sqrt函數(shù):返回一個(gè)數(shù)字的平方根值。?
(18)tan函數(shù):返回一個(gè)數(shù)字的正切值。?
5.字符串函數(shù)?
javascript字符串函數(shù)完成對字符串的字體大小、顏色、長度和查找等操作,共包括以下20個(gè)函數(shù):?
(1)anchor函數(shù):產(chǎn)生一個(gè)鏈接點(diǎn)(anchor)以作超級鏈接用。anchor函數(shù)設(shè)定的鏈接點(diǎn)的名稱,另一個(gè)函數(shù)link設(shè)定的URL地址。?
(2)big函數(shù):將字體加到一號,與...標(biāo)簽結(jié)果相同。?
(3)blink函數(shù):使字符串閃爍,與...標(biāo)簽結(jié)果相同。?
(4)bold函數(shù):使字體加粗,與...標(biāo)簽結(jié)果相同。?
(5)charAt函數(shù):返回字符串中指定的某個(gè)字符。?
(6)fixed函數(shù):將字體設(shè)定為固定寬度字體,與...標(biāo)簽結(jié)果相同。?
(7)fontcolor函數(shù):設(shè)定字體顏色,與標(biāo)簽結(jié)果相同。?
(8)fontsize函數(shù):設(shè)定字體大小,與標(biāo)簽結(jié)果相同。?
(9)indexOf函數(shù):返回字符串中第一個(gè)查找到的下標(biāo)index,從左邊開始查找。?
(10)italics函數(shù):使字體成為斜體字,與...標(biāo)簽結(jié)果相同。?
(11)lastIndexOf函數(shù):返回字符串中第一個(gè)查找到的下標(biāo)index,從右邊開始查找。?
(12)length函數(shù):返回字符串的長度。(不用帶括號)?
(13)link函數(shù):產(chǎn)生一個(gè)超級鏈接,相當(dāng)于設(shè)定的URL地址。?
(14)small函數(shù):將字體減小一號,與...標(biāo)簽結(jié)果相同。?
(15)strike函數(shù):在文本的中間加一條橫線,與...標(biāo)簽結(jié)果相同。?
(16)sub函數(shù):顯示字符串為下標(biāo)字(subscript)。?
(17)substring函數(shù):返回字符串中指定的幾個(gè)字符。?
(18)sup函數(shù):顯示字符串為上標(biāo)字(superscript)。?
(19)toLowerCase函數(shù):將字符串轉(zhuǎn)換為小寫。?
(20)toUpperCase函數(shù):將字符串轉(zhuǎn)換為大寫。
事件源對象?
event.srcElement.tagName?
event.srcElement.type?
捕獲釋放?
event.srcElement.setCapture();?
event.srcElement.releaseCapture();?
事件按鍵?
event.keyCode?
event.shiftKey?
event.altKey?
event.ctrlKey?
事件返回值?
event.returnValue?
鼠標(biāo)位置?
event.x?
event.y?
窗體活動元素?
document.activeElement?
綁定事件?
document.captureEvents(Event.KEYDOWN);?
訪問窗體元素?
document.all("txt").focus();?
document.all("txt").select();?
窗體命令?
document.execCommand?
窗體COOKIE?
document.cookie?
菜單事件?
document.oncontextmenu?
創(chuàng)建元素?
document.createElement("SPAN");?
根據(jù)鼠標(biāo)獲得元素:?
document.elementFromPoint(event.x,event.y).tagName=="TD?
document.elementFromPoint(event.x,event.y).appendChild(ms)?
窗體圖片?
document.images[索引]?
窗體事件綁定?
document.οnmοusedοwn=scrollwindow;?
元素?
document.窗體.elements[索引]?
對象綁定事件?
document.all.xxx.detachEvent('onclick',a);?
插件數(shù)目?
navigator.plugins?
取變量類型?
typeof($js_libpath) == "undefined"?
下拉框?
下拉框.options[索引]?
下拉框.options.length?
查找對象?
document.getElementsByName("r1");?
document.getElementById(id);?
定時(shí)?
timer=setInterval('scrollwindow()',delay);?
clearInterval(timer);?
UNCODE編碼?
escape() ,unescape?
父對象?
obj.parentElement(dhtml)?
obj.parentNode(dom)?
交換表的行?
TableID.moveRow(2,1) //將第三行與第二行交換
替換CSS?
document.all.csss.href = "a.css";?
并排顯示?
display:inline?
隱藏焦點(diǎn)?
hidefocus=true?
根據(jù)寬度換行?
style="word-break:break-all"?
自動刷新?
<meta HTTP-EQUIV="refresh" CONTENT="8;URL=http://hi.baidu.com">?
簡單郵件?
<a href="mailto:jishu@bj08ay.cn?subject=thisistitle&body=thisisthewordswhatyouwanttowriteforme">?
快速轉(zhuǎn)到位置?
obj.scrollIntoView(true)?
錨?
<a name="first">?
<a href="#first">anchors</a>?
網(wǎng)頁傳遞參數(shù)?
location.search();?
可編輯?
obj.contenteditable=true?
執(zhí)行菜單命令?
obj.execCommand?
雙字節(jié)字符?
/[^\x00-\xff]/?
漢字?
/[\u4e00-\u9fa5]/?
讓英文字符串超出表格寬度自動換行?
word-wrap: break-word; word-break: break-all;?
透明背景?
<IFRAME src="1.htm" width=300 height=180 allowtransparency></iframe>?
獲得style內(nèi)容?
obj.style.cssText?
HTML標(biāo)簽?
document.documentElement.innerHTML?
第一個(gè)style標(biāo)簽?
document.styleSheets[0]?
style標(biāo)簽里的第一個(gè)樣式?
document.styleSheets[0].rules[0]?
防止點(diǎn)擊空鏈接時(shí),頁面往往重置到頁首端。?
<a href="javascript:function()">word</a>?
上一網(wǎng)頁源?
asp:?
request.servervariables("HTTP_REFERER")?
javascript:?
document.referrer?
釋放內(nèi)存?
CollectGarbage();?
禁止右鍵?
document.oncontextmenu = function() { return false;}?
禁止保存?
<noscript><iframe src="*.htm"></iframe></noscript>?
禁止選取
<body οncοntextmenu="return false" οndragstart="return false" onselectstart ="return false" οnselect="document.selection.empty()" οncοpy="document.selection.empty()" onbeforecopy="return false"οnmοuseup="document.selection.empty()>?
禁止粘貼?
<input type=text οnpaste="return false">?
地址欄圖標(biāo)?
<link rel="Shortcut Icon" href="favicon.ico">?
favicon.ico 名字最好不變16*16的16色,放虛擬目錄根目錄下?
收藏欄圖標(biāo)?
<link rel="Bookmark" href="favicon.ico">?
查看源碼?
<input type=button value=查看網(wǎng)頁源代碼 οnclick="window.location = 'view-source:'+ 'http://www.csdn.net/'">?
關(guān)閉輸入法?
<input style="ime-mode:disabled">
自動全選?
<input type=text name=text1 value="123" οnfοcus="this.select()">?
ENTER鍵可以讓光標(biāo)移到下一個(gè)輸入框?
<input οnkeydοwn="if(event.keyCode==13)event.keyCode=9">?
文本框的默認(rèn)值?
<input type=text value="123" οnfοcus="alert(this.defaultValue)">?
獲得時(shí)間所代表的微秒?
var n1 = new Date("2004-10-10".replace(/-/g, "\/")).getTime()?
窗口是否關(guān)閉?
win.closed?
checkbox扁平?
<input type=checkbox style="position: absolute; clip:rect(5px 15px 15px 5px)"><br>?
獲取選中內(nèi)容?
document.selection.createRange().duplicate().text?
自動完成功能?
<input type=text autocomplete=on>打開該功能?
<input type=text autocomplete=off>關(guān)閉該功能?
窗口最大化?
<body οnlοad="window.resizeTo(window.screen.width - 4,window.screen.height-50);window.moveTo(-4,-4)">?
無關(guān)閉按鈕IE?
window.open("aa.htm", "meizz", "fullscreen=7");?
統(tǒng)一編碼/解碼?
alert(decodeURIComponent(encodeURIComponent("http://你好.com?as= hehe")))?
encodeURIComponent對":"、"/"、";" 和 "?"也編碼?
表格行指示?
<tr οnmοuseοver="this.bgColor='#f0f0f0'" οnmοuseοut="this.bgColor='#ffffff'">?
各種尺寸
s += "\r\n網(wǎng)頁可見區(qū)域?qū)?#xff1a;"+ document.body.clientWidth;?
s += "\r\n網(wǎng)頁可見區(qū)域高:"+ document.body.clientHeight;?
s += "\r\n網(wǎng)頁可見區(qū)域高:"+ document.body.offsetWeight +" (包括邊線的寬)";?
s += "\r\n網(wǎng)頁可見區(qū)域高:"+ document.body.offsetHeight +" (包括邊線的寬)";?
s += "\r\n網(wǎng)頁正文全文寬:"+ document.body.scrollWidth;?
s += "\r\n網(wǎng)頁正文全文高:"+ document.body.scrollHeight;?
s += "\r\n網(wǎng)頁被卷去的高:"+ document.body.scrollTop;?
s += "\r\n網(wǎng)頁被卷去的左:"+ document.body.scrollLeft;?
s += "\r\n網(wǎng)頁正文部分上:"+ window.screenTop;?
s += "\r\n網(wǎng)頁正文部分左:"+ window.screenLeft;?
s += "\r\n屏幕分辨率的高:"+ window.screen.height;?
s += "\r\n屏幕分辨率的寬:"+ window.screen.width;?
s += "\r\n屏幕可用工作區(qū)高度:"+ window.screen.availHeight;?
s += "\r\n屏幕可用工作區(qū)寬度:"+ window.screen.availWidth;
from:?http://www.jb51.net/article/21661.htm
總結(jié)
以上是生活随笔為你收集整理的JavaScript 常用函数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一次生产事故的优化经历
- 下一篇: javascript常用函数和技巧