日期:2014-05-16  浏览次数:20278 次

【转】js常用函数整理
原帖:http://www.cnblogs.com/qiantuwuliang/archive/2009/08/26/1554409.html

类型转换: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的最后。

举例: