10大javascript函数
来源:互联网 发布:17年网络群体事件 编辑:程序博客网 时间:2024/06/05 12:39
dustindiaz好像是米国的牛人,搜索YUI时看到他的网站,尤其是看到他用YUI的DOM和EVENT编写的俄罗斯方块(YUI Tetris)时(据说只用了两天半时间),那个牛,无法用现在的我来衡量^-^
翻了他的一篇Top 10 custom JavaScript functions of all time,(十大常用js函数)觉得不错,以下是简略介绍:
10) addEvent() 给对象添加行为
function addEvent(elm, evType, fn, useCapture) {
if (elm.addEventListener) {
elm.addEventListener(evType, fn, useCapture);
return true;
}
else if (elm.attachEvent) {
var r = elm.attachEvent('on' + evType, fn);
return r;
}
else {
elm['on' + evType] = fn;
}
}
9) addLoadEvent() 添加自动加载行为
function addLoadEvent(func) {var oldonload = window.onload;if (typeof window.onload != 'function') {window.onload = func;}else {window.onload = function() {oldonload();func();}}}
8) getElementsByClass() 根据className获取对象
function getElementsByClass(searchClass,node,tag) {var classElements = new Array();if ( node == null )node = document;if ( tag == null )tag = '*';var els = node.getElementsByTagName(tag);var elsLen = els.length;var pattern = new RegExp('(^|//s)'+searchClass+'(//s|$)');for (i = 0, j = 0; i < elsLen; i++) {if ( pattern.test(els[i].className) ) {classElements[j] = els[i];j++;}}return classElements;}
7) cssQuery() 查询对象的css属性
6) toggle() 显示或隐藏对象元素
function toggle(obj) {var el = document.getElementById(obj);if ( el.style.display != 'none' ) {el.style.display = 'none';}else {el.style.display = '';}}
5) insertAfter() 将节点插入到某节点前
function insertAfter(parent, node, referenceNode) {parent.insertBefore(node, referenceNode.nextSibling);}
4) inArray() 判断数组里是否具有某值
Array.prototype.inArray = function (value) {var i;for (i=0; i < this.length; i++) {if (this[i] === value) {return true;}}return false;};
3, 2, & 1) getCookie(), setCookie(), deleteCookie() Cookie的读写设操作
function getCookie( name ) {var start = document.cookie.indexOf( name + "=" );var len = start + name.length + 1;if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {return null;}if ( start == -1 ) return null;var end = document.cookie.indexOf( ';', len );if ( end == -1 ) end = document.cookie.length;return unescape( document.cookie.substring( len, end ) );}function setCookie( name, value, expires, path, domain, secure ) {var today = new Date();today.setTime( today.getTime() );if ( expires ) {expires = expires * 1000 * 60 * 60 * 24;}var expires_date = new Date( today.getTime() + (expires) );document.cookie = name+'='+escape( value ) +( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()( ( path ) ? ';path=' + path : '' ) +( ( domain ) ? ';domain=' + domain : '' ) +( ( secure ) ? ';secure' : '' );}function deleteCookie( name, path, domain ) {if ( getCookie( name ) ) document.cookie = name + '=' +( ( path ) ? ';path=' + path : '') +( ( domain ) ? ';domain=' + domain : '' ) +';expires=Thu, 01-Jan-1970 00:00:01 GMT';}
- 10大javascript函数
- JavaScript trim函数大赏
- JavaScript trim函数大赏
- JavaScript trim函数大赏
- JavaScript小程序,大作用(函数)
- JavaScript:关于document.execCommand()函数可用参数大解析
- 很有用的JavaScript函数以及基础写法大集合
- javascript之小程序大作用(函数)
- JavaScript: 100多个很有用的JavaScript函数以及基础写法大集合
- 原生JavaScript技巧大收集(1~10)
- 大函数拆成多个小函数
- javascript函数
- Javascript函数
- Javascript函数
- javascript函数
- javascript函数
- javascript函数
- JavaScript函数
- sql server jdbc连接
- 正则表达式语法
- 剖析Linux系统中硬链接与软链接的区别
- 学点js
- jdom 解析xml文件
- 10大javascript函数
- silverlight - 获取鼠标滚轮事件 及 判断获取组合键的方法
- LINQ 查询指定库中所有表名称的实现
- Android提高十七篇之多级树形菜单的实现
- SWT中判断组合键
- convert from dd/mm/yyyy to mm/dd/yyyy in c#
- iBATI_动态映射
- AOP入门 - 1
- 开发竞赛作品展示网站上线!