js常用函數整理

 

類型轉換:parseInt\parseFloat\toString

類型判斷:typeof;eg:if(typeof(var)!="undefined")\isNaN

字符處理函數:toLowerCase\touppercase\ 
substring(從哪裏截到哪)\substr(從哪裏開始;截多少個字符) \indexOf(字串中找某串字符)\replace(替換函數)\
charCodeAt
concat
split
join

定時處理函數:setInterval;setTimeout;clearTimeout;clearInterval

兩個顯示控制屬性:visibility(hidden,hide,show,visible);display(none,block,inline)

編碼的一些函數:escape();

取得對象的方法:
document.getElementsByTagName(根據標籤名獲得對象組)\
document.getElementsByTagName('head').item(0)
document.getElementsByName(通過NAME獲得對象)\
document.getElementById(通過ID獲得對象)\
document.form1.f1\
document.all.tags(t)[id]
document.all[id]
document.layers[id]

不常用的一些方法:
moveToAbsolute:
fromElement
toElement
contains
captureEvents
parent.frames
moveTo(x,y)
resizeBy(x,y)
resizeTo(x,y)
attachEvent('onmousedown',showHide);
document.createElement('')
document.createTextNode('')
obj.setAttribute('src','')
obj.getAttribute('')
event.srcElement
style.cssText
document.body.appendChild
document.body.removeChild
document.body.insertAdjacentHTML("AfterBegin",s)
innerHTML/
removeNode
insertBefore
arguments
event.keyCode

正則相關:RegExp(eg:new RegExp("\"","g"))

一些長寬及位置屬性:
window.innerHeight
window.innerWidth
document.documentElement.clientWidth
document.documentElement.clientHeight
document.body.clientWidth
document.body.clientHeight
document.body.offsetWidth
document.body.offsetHeight
document.body.scrollLeft
document.body.scrollTop
document.location.reload()

window一些方法:
window.onerror=function(){return true;}隱去報錯
window.onload\window.onunload
window.status
window.parent.location.href
window.location.href
window.top.location.href

數學方法:
Math.max

數組方法:
length
slice

JS輸出方法:
document.write(s);
alert(s);
confirm
prompt
document.writeln
彈窗方法:
window.showModalDialog(<url>,<標題>,<屬性>)
window.open("","","");


附:常用DOM方法和屬性(即必須掌握部分)

創建節點:createElement();createTextNode()
複製節點:cloneNode()
插入節點:appendChild();insertBefore()
刪除節點:removeChild()
替換節點:replaceChild()
設置節點屬性:setAttribute()
查找節點:getAttribute();getElementById();getElementsByTagName();hasChildNodes
節點的屬性:nodeName;nodeType;nodeValue
遍歷節點樹:childNodes;firstChild;lastChild;nextSibling;parentNode;previousSibling

 

 

 

 

 

js中substring和substr的用法

 

 

 

1.substring 方法

定義和用法

substring 方法用於提取字符串中介於兩個指定下標之間的字符。

語法

stringObject.substring(start,stop)

參數     描述
start     必需。一個非負的整數,規定要提取的子串的第一個字符在 stringObject 中的位置。
stop     可選。一個非負的整數,比要提取的子串的最後一個字符在 stringObject 中的位置多 1。如果省略該參數,那麼返回的子串會一直到字符串的結尾。

返回值

一個新的字符串,該字符串值包含 stringObject 的一個子字符串,其內容是從 start 處到 stop-1 處的所有字符,其長度爲 stop 減 start。

說明

substring 方法返回的子串包括 start 處的字符,但不包括 end 處的字符。
如果 start 與 end 相等,那麼該方法返回的就是一個空串(即長度爲 0 的字符串)。
如果 start 比 end 大,那麼該方法在提取子串之前會先交換這兩個參數。
如果 start 或 end 爲負數,那麼它將被替換爲 0。

2.substr 方法

定義和用法

substr 方法用於返回一個從指定位置開始的指定長度的子字符串。

語法

stringObject.substr(start [, length ])

參數    描述
start   必需。所需的子字符串的起始位置。字符串中的第一個字符的索引爲 0。
length 可選。在返回的子字符串中應包括的字符個數。

說明

如果 length 爲 0 或負數,將返回一個空字符串。
如果沒有指定該參數,則子字符串將延續到stringObject的最後。

 

 

舉例:
var str = "0123456789";

alert(str.substring(0));------------"0123456789"
alert(str.substring(5));------------"56789"
alert(str.substring(10));-----------""
alert(str.substring(12));-----------""
alert(str.substring(-5));-----------"0123456789"
alert(str.substring(-10));----------"0123456789"
alert(str.substring(-12));----------"0123456789"
alert(str.substring(0,5));----------"01234"
alert(str.substring(0,10));---------"0123456789"
alert(str.substring(0,12));---------"0123456789"
alert(str.substring(2,0));----------"01"
alert(str.substring(2,2));----------""
alert(str.substring(2,5));----------"234"
alert(str.substring(2,12));---------"23456789"
alert(str.substring(2,-2));---------"01"
alert(str.substring(-1,5));---------"01234"
alert(str.substring(-1,-5));--------""

alert(str.substr(0));---------------"0123456789"
alert(str.substr(5));---------------"56789"
alert(str.substr(10));--------------""
alert(str.substr(12));--------------""
alert(str.substr(-5));--------------"0123456789"
alert(str.substr(-10));-------------"0123456789"
alert(str.substr(-12));-------------"0123456789"
alert(str.substr(0,5));-------------"01234"
alert(str.substr(0,10));------------"0123456789"
alert(str.substr(0,12));------------"0123456789"
alert(str.substr(2,0));-------------""
alert(str.substr(2,2));-------------"23"
alert(str.substr(2,5));-------------"23456"
alert(str.substr(2,12));------------"23456789"
alert(str.substr(2,-2));------------""
alert(str.substr(-1,5));------------"01234"
alert(str.substr(-1,-5));-----------""   
   
參考文獻:
http://www.w3school.com.cn/js/jsref_substring.asp
http://www.w3school.com.cn/js/jsref_substr.asp

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章