(7)inArray()
用於判斷檢查數組中是否存在某個值,下面方法取自Prototype類庫。
- Array.prototype.inArray = function (value) {
- for (var i=0,l = this.length ; i <l ; i++) {
- if (this[i] === value) {
- return true;
- }
- }
- return false;
- };
另一個版本:
- var inArray = function (arr,value) {
- for (var i=0,l = arr.length ; i <l ; i++) {
- if (arr[i] === value) {
- return true;
- }
- }
- return false;
- };
(8) getCookie(), setCookie(), deleteCookie()
做BBS與商業網站的應該經常用到,無理由每次都要讓用戶輸入密碼登錄吧。我們需要藉助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‘;
- }
(9)getStyle()與setStyle()
所有UI控件都應該存在的函數,動態設置樣式與獲取樣式。這個可以寫得很短,也可以寫得很長,但要精確取得樣式,一個字:難!但我發現許多問題都是發端於IE,微軟的開發人員好像從來不打算給出getComputedStyle這樣的函數,與之相近的currentStyle會返回auto,inhert, ‘ ‘等讓你哭笑不得的值,這還沒有算上IE怪癖模式帶來的難度呢!各類庫的實現是非常長與難分離出來的,下面是我實現的版本:
- function setStyle(el,prop,value){
- if(prop == "opacity" && !+"\v1"){
- //IE7 bug:filter 濾鏡要求 hasLayout=true 方可執行(否則沒有效果)
- if (!el.currentStyle || !el.currentStyle.hasLayout) el.style.zoom = 1;
- prop = "filter";
- if(!!window.XDomainRequest){
- value ="progid:DXImageTransform.Microsoft.Alpha(style=0,opacity="+value*100+")";
- }else{
- value ="alpha(opacity="+value*100+")"
- }
- }
- el.style.cssText += ‘;‘ + (prop+":"+value);
- }
- function getStyle(el, style){
- if(!+"\v1"){
- style = style.replace(/\-(\w)/g, function(all, letter){
- return letter.toUpperCase();
- });
- return el.currentStyle[style];
- }else{
- return document.defaultView.getComputedStyle(el, null).getPropertyValue(style)
- }
- }
有關setStyle還可以看我另一篇博文,畢竟現在設置的樣式都是內聯樣式,與html混雜在一起。
(10)$()
實至名歸,最值錢的函數,可以節省多少流量啊。最先由Prototype.js實現的,那是洪荒時代遺留下來的珍獸,現在有許多變種。
- function $() {
- var elements = [];
- for (var i = 0; i < arguments.length; i++) {
- var element = arguments[i];
- if (typeof element == ‘string‘)
- element = document.getElementById(element);
- if (arguments.length == 1)
- return element;
- elements.push(element);
- }
- return elements;
- }
十個最常用的javascript自定義函數(1):
http://blog.csdn.net/d19891006/article/details/6938887
原文地址:http://www.cnblogs.com/rubylouvre/archive/2009/09/02/1558998.html