部分也是本身經常使用的,可是部分還須要實際驗證 一、Array類型函數:
array.concat(item…) 函數功能:關聯數組,實現數組相加功能,但並不影響原先數組,concat返回新數組。 JS經常使用的標準函數 array.join(separator) 函數功能:將array中的每一個元素用separator爲分隔符構形成字符串,默認的separator是逗號,固然你能夠本身指定separator 事實上join方法最經常使用的地方在字符串相加,用過js的人都知道js的字符串相加是特別慢且性能特別差的,把大量片斷字符串放在一個數組中並用join方法鏈接比用+鏈接要快得多。 JS經常使用的標準函數 array.pop() pop和push方法使數組array像堆棧同樣工做。 函數功能:pop方法移除array中的最後一個元素並返回該元素。 若是array是空的,則返回undefined; JS經常使用的標準函數 array.push(item…) 函數功能:push方法將一個或多個item參數附加到array尾部。可是它不像concat方法那樣,push方法不會修改item參數,若是參數item是一個數組,它會將參數數組做爲單個元素整個添加到數組中,它返回array的新長度 JS經常使用的標準函數 array.reverse() 函數功能:反轉array中元素的順序,返回當前array array.shift() 函數功能:移除array中的第一個元素並返回該元素,若是array是空的,它會返回undefined。 shift比pop要慢不少 array.slice(start,end) 函數功能:對array中的一段作淺複製。end參數是可選的,默認值爲數組的長度array.length。 若是兩個參數中任何一個是負數,array.length將和它們相加來試圖將它們成爲非負數。 若是start大於等於array.length,獲得的結果將是一個新的空數組 JS經常使用的標準函數 array.sort(compareFn) 函數功能:對array中的內容進行排序。 var n = [4, 8, 15, 16, 23, 43]; n.sort(function (a, b) { return a - b; }); console.log(n); //[4, 8, 15, 16, 23, 43]
var m = ['aa', 'bb', 'a', 4, 8, 15, 16, 23, 43]; m.sort(function (a, b) { if (a === b) { return 0; } if (typeof a === typeof b) { return a < b ? -1 : 1; } return typeof a < typeof b ? -1 : 1; }); console.log(m); //[4, 8, 15, 16, 23, 43, "a", "aa", "bb"]
var compare = function(name) { return function(o, p) { var a, b; if (typeof o === 'object' && typeof p === 'object' && o && p) { a = o[name]; b = p[name]; if (a === b) { return 0; } if (typeof a === typeof b) { return a < b ? -1 : 1; } return typeof a < typeof b ? -1 : 1; } else { throw { name : "Error", message : 'Expected an object when sorting by ' + name }; } }; }; var s = [ { first : 'Joe', last : 'Felix' }, { first : 'Moe', last : 'Besser' }, { first : 'Joe', last : 'Howard' }, { first : 'Shemp', last : 'DeRita' }, { first : 'Larry', last : 'Howard' }, { first : 'Curly', last : 'Fine' }, { first : 'Shirly', last : 'Angela' } ]; s.sort(compare('first')); /* * s = [ { first: 'Curly', last: 'Fine' }, { first: 'Joe', last: 'Felix' }, { * first: 'Joe', last: 'Howard' }, { first: 'Larry', last: 'Howard' }, { first: * 'Moe', last: 'Besser' }, { first: 'Shemp', last: 'DeRita' }, { first: * 'Shirly', last: 'Angela' } ]; */
array.splice(start,deleteCount,item…) 函數功能:從array中移除1個或多個元素,並用新的item替換它們。返回一個包含移除元素的數組。 array.unshift(item…) 函數功能:將item插入到array的開始部位。返回array的新長度值。 2、Function類型函數 function.apply(thisArg,argArray) Function.prototype.bind = function(that) { // 返回一個函數,調用這個函數就像它是那個對象的方法同樣
var method = this, slice = Array.prototype.slice, args = slice.apply( arguments, [ 1 ]); return function() { return method.apply(that, args.concat(slice.apply(arguments, [ 0 ]))); }; }; var x = function() { return this.value; }.bind({ value : 666 }); console.log(x());// 666
3、Number類型函數 number.toExponential(fractionDigits) 函數功能:把number轉換成一個指數形式的字符串。可選參數fractionDigits控制其小數點後的數字位數,它的值必須在0到20之間。 JS經常使用的標準函數 number.toFixed(fractionDigits) 函數功能:把number轉換成一個十進制形式的字符串。可選參數fractionDigits控制其小數點後的數字位數,它的值必須在0到20之間。默認爲0。 JS經常使用的標準函數 number.toPrecision(precision) 函數功能:把number轉換成一個十進制形式的字符串。可選參數fractionDigits控制其小數點後的數字位數,它的值必須在1到21之間。 JS經常使用的標準函數 number.toString(radix) 函數功能:把number轉換成一個字符串。可靠參數radix控制基數,它的值必須在2和36之間,默認爲10 JS經常使用的標準函數 4、Object類型函數 object.hasOwnProperty(name) 函數功能:若是object包含名爲name的屬性,hasOwnProperty方法將返回true,原型鏈中的同名屬性是不會被檢查的。 for (name in obj) { if (obj.hasOwnProperty(name)) { console.log(obj[name]); } } 5、Regexp類型函數 regexp.exec(string) 函數功能:成功匹配regexp和字符串,返回一個數組。 數組中下標爲0的元素包含正則表達式regexp匹配的子字符串 下標爲1的元素是分組1捕獲的文本,下標爲2的元素是分組2捕獲的文本,依此類推。若是匹配失敗,就返回null。 若是regexp帶有一個g(全局)標誌,查找不是從這個字符串起始位置開始,而是從regexp.lastIndex(它的初始值爲0)位置開始,若是匹配成功,那麼regexp.lastIndex將被設置爲該匹配後第一個字符的位置。 不成功的匹配會重置regexp.lastIndex爲0 regexp.test(string) 函數功能:若是regexp匹配string,返回true 不然返回false 6、String類型函數 string.charAt(pos) 函數功能:返回在string中pos位置處的字符。 若是pos小於0或者大於string.length,將返回空字符串 string.charCodeAt(pos) 函數功能:返回在string中pos位置處的字符的字符編碼。 string.concat(string…) 函數功能:鏈接字符串(不過不多被使用,由於用運算符+也能實現這個功能) string.indexOf(searchString,position) 函數功能:在string內查找另外一個字符串searchString,若是找到則返回第一個匹配字符的位置,不然返回-1。 可選參數position可設置從string的某個位置開始查找。 string.lastIndexOf(searchString,position) 函數功能:跟indexOf方法相似,區別是從字符串的末尾開始查找而不是從開頭。 string.localCompare(that) 函數功能:比較兩個字符串 string.match(regexp) 函數功能:匹配一個字符串和一個正則表達式。 若是沒有g標識,string.match(regexp)和regexp.exec(string)結果相同。 若是有g標識,返回一個包含除捕獲分組之處的全部匹配的數組。 string.replace(searchValue,replaceValue) 函數功能:對string進行查找和替換操做,並返回一個新的字符串。 var oldAreaCode = /\((\d{3})\)/g; var p = '(555)(666)-1212'.replace(oldAreaCode, '$1-'); console.log(p);//555-666--1212
string.search(regexp) 函數功能:和indexOf方法相似,只是它接受一個正則表達式做爲參數而不是一個字符串,若是找到匹配返回第一個匹配的首字符位置,若是沒有找到匹配,則返回-1。 string.slice(start,end) 函數功能:複製string的一部分來構造一個新的字符串。 若是start參數是負數,它將與string.length相加。end參數是可選的,而且它的默認值是string.length。若是end參數是負數,它將與string.length相加。end參數是一個比最末一個字符的位置值還大的數。 string.split(separator,limit) 函數功能:把string分割成片斷來建立一個字符串數組。可選參數limit能夠限制被分割的片斷數量。 var digits = '0123456789'; var a = digits.split('', 5); //["0", "1", "2", "3", "4"]
string.substring(start,end) 函數功能:substring的用法和slice方法同樣,只是它不能處理負數參數,通常性的用slice替代它。 string.toLocalLowerCase() 函數功能:用本地化的規則把string中的全部字母轉換爲小寫格式。 string.toLocalUpperCase() 函數功能:用本地化的規則把string中的全部字母轉換爲大寫格式。 string.toLowerCase() 函數功能: 把string中的全部字母都轉化爲小寫格式。 string.toUpperCase() 函數功能: 把string中的全部字母都轉化爲大格式。 string.fromCharCode(char…) 函數功能:從一串數字中返回一個字符串。 var a = String.fromCharCode(67, 97, 116); console.log(a);//Cat;
/** *javascript按字節進行截取 * * param str 要截取的字符串 * param L 要截取的字節長度,注意是字節不是字符,一個漢字兩個字節 * return 截取後的字符串 */
function cutStr(str,L){ var result = '',
strlen = str.length, // 字符串長度
chrlen = str.replace(/[^\x00-\xff]/g,'**').length; // 字節長度
if(chrlen<=L){return str;} for(var i=0,j=0;i<strlen;i++){ var chr = str.charAt(i); if(/[\x00-\xff]/.test(chr)){ j++; // ascii碼爲0-255,一個字符就是一個字節的長度
}else{ j+=2; // ascii碼爲0-255之外,一個字符就是兩個字節的長度
} if(j<=L /* || j==L+1 */){ // 當加上當前字符之後,若是總字節長度小於等於L,則將當前字符真實的+在result後
result += chr; }else{ // 反之則說明result已是不拆分字符的狀況下最接近L的值了,直接返回
return result; } } } // 用例
alert(cutStr("abc中英文混合",10)); //獲取firstchild
function firstChild(elem) { var node = elem.firstChild ? elem.firstChild : null; while(node && node.nodeType != 1) { node = node.nextSibling; } return node; } //獲取下一個同輩元素(ie下回忽略空白文節點)
function next(elem) { var node = elem.nextSibling ? elem.nextSibling : null; while(node && node.nodeType != 1 ) { node = node.nextSibling; } return node; } //獲取上一個同輩元素(ie下回忽略空白文節點)
function prev(elem) { var node = elem.previousSibling ? elem.previousSibling : null; while(node && node.nodeType != 1 ) { node = node.previousSibling; } return node; } /** * 獲取指定類名的父元素 * @param elem * @param className * @returns */
function parents(elem, className) { //檢測自身是否知足條件
if(elem.className.indexOf(className) != -1) { return elem; } var parent = null; while((parent = elem.parentNode) && parent.nodeType == 1 && !!className) { if(parent.className.indexOf(className) != -1) { break; } elem = parent; } return parent; } //輸入html代碼,輸出轉換後的代碼
function htmlEncode( html ) { //使用div來模擬
var div = document.createElement("div"); if(typeof div.innerText != 'undefined') { div.innerText = html; }else{ div.textContent = html; } var html_encode = div.innerHTML; div = null; return html_encode; } function htmlDecode( html ) { //使用div來模擬
var text, div = document.createElement("div"); div.innerHTML = html; if(typeof div.innerText != 'undefined') { text = div.innerText; }else{ text = div.textContent; } return text; } function htmlEncode(str) { var s = ""; if (str.length == 0) return ""; s = str.replace(/\&/g, "&"); s = s.replace(/</g, "<"); s = s.replace(/>/g, ">"); s = s.replace(/\'/g, "'"); s = s.replace(/\"/g, """); return s; } function htmlDecode(str) { var s = ""; if (str.length == 0) return ""; s = str.replace(/&/g, "&"); s = s.replace(/</g, "<"); s = s.replace(/>/g, ">"); s = s.replace(/'/g, "\'"); s = s.replace(/"/g, "\""); return s; } function htmlEncode(html) { var div = document.createElement("div"); var text = document.createTextNode(""); div.appendChild(text); text.nodeValue=html; return div.innerHTML; } function htmlDecode(str) { var div = document.createElement("div"); div.innerHTML = str; return div.firstChild.nodeValue; } /** ****/ //獲取scrolltop function getScrollTop() { var scrollTop = document.documentElement.scrollTop || document.body.scrollTop; return scrollTop; } //設置scrolltop function setScrollTop(top) { document.documentElement.scrollTop = document.body.scrollTop = top; } /** 獲取瀏覽器可視區域的尺寸 */ function getBrowserSize() { //在標準模式下用 documentElement, 在怪癖模式下用 body return { width:document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth, height:document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight, } } /** 獲取頁面大小 */ function getPageSize() { //ie下優先取 body return { width:document.body.scrollWidth ? document.body.scrollWidth : document.documentElement.scrollWidth, height: document.body.scrollHeight ? document.body.scrollHeight : document.documentElement.scrollHeight } } /** 獲取元素到頁面頂端的距離(出自jquery源碼) */ function getCoords(el) { if (typeof el == 'string') { el = Fid(el); } var box = el.getBoundingClientRect(), doc = el.ownerDocument, body = doc.body, html = doc.documentElement, clientTop = html.clientTop || body.clientTop || 0, clientLeft = html.clientLeft || body.clientLeft || 0, top = box.top + (self.pageYOffset || html.scrollTop || body.scrollTop) - clientTop, left = box.left + (self.pageXOffset || html.scrollLeft || body.scrollLeft) - clientLeft return { 'top' : top, 'left' : left }; }; /* 獲取元素到頁面頂端的 document.documentElement.clientLeft ie6/7 下2px的bodrer */ function getAbsPoint(e) { if (typeof e == 'string') { e = Fid(e); } var x = e.offsetLeft; var y = e.offsetTop; var de = document.documentElement; while (e = e.offsetParent) { //在IE下offset對象是對當前元素到上一級元素的距離,FF則是正常的 x += e.offsetLeft; y += e.offsetTop; //須要加上元素自身的border, offsetTop 自己就包含margin if (e && e != de && !Browser.isOpera) { x += e.clientLeft; y += e.clientTop; //ie6下設置元素寬度後才能到獲取他的邊框大小,若是不設置則下級取上級的時候會把上級的邊框算進去(整體計算仍是沒有錯) } } return { "left" : x, "top" : y }; } //瀏覽器類型檢測 var FBrowser=(function(){ var ua = navigator.userAgent; var isOpera = Object.prototype.toString.call(window.opera) == '[object Opera]'; return { isIE: !!window.attachEvent && window.ActiveXObject && !isOpera, isOpera: isOpera, isSafari: ua.indexOf('AppleWebKit/') > -1, isFirefox: ua.indexOf('Gecko') > -1 && ua.indexOf('KHTML') === -1, MobileSafari: /Apple.*Mobile.*Safari/.test(ua), isChrome: !!window.chrome, }; })(); FBrowser.isIE6=FBrowser.isIE&&!window.XMLHttpRequest; FBrowser.isIE7=FBrowser.isIE&&!!window.XMLHttpRequest; //document.documentMode ie8+以上顯示瀏覽器版本 var IE = (function(){ var ie = !!window.ActiveXObject; e = { isIE: !!window.ActiveXObject, isIE6: ie && !window.XMLHttpRequest, isIE7: ie && window.XMLHttpRequest && document.documentMode == 7, isIE8: ie && window.XMLHttpRequest && document.documentMode == 8, isIE9: ie && window.XMLHttpRequest && document.documentMode == 9 } return e; })(); /** 獲取元素樣式 處理透明度、元素浮動樣式的獲取 ,結果帶有單位 */ function getStyle(elem, name) { var nameValue = null; if (document.defaultView) { var style = document.defaultView.getComputedStyle(elem, null); nameValue = name in style ? style[name] : style.getPropertyValue(name); } else { var style = elem.style, curStyle = elem.currentStyle; //透明度 from youa if (name == "opacity") { if (/alpha\(opacity=(.*)\)/i.test(curStyle.filter)) { var opacity = parseFloat(RegExp.$1); return opacity ? opacity / 100 : 0; } return 1; } if (name == "float") { name = "styleFloat"; } var ret = curStyle[name] || curStyle[camelize(name)]; //單位轉換 from jqury if (!/^-?\d+(?:px)?$/i.test(ret) && /^\-?\d/.test(ret)) { var left = style.left, rtStyle = elem.runtimeStyle, rsLeft = rtStyle.left; rtStyle.left = curStyle.left; style.left = ret || 0; ret = style.pixelLeft + "px"; style.left = left; rtStyle.left = rsLeft; } nameValue = ret; } return nameValue === 'auto' ? '0px' : nameValue; } function uncamelize(s) {//將CSS屬性名由駝峯式轉爲普通式 return s.replace(/[A-Z]/g,function (c) { return '-'+c.charAt(0).toLowerCase(); }); } function camelize(s) {//將CSS屬性名轉換成駝峯式 return s.replace(/-[a-z]/gi,function (c) { return c.charAt(1).toUpperCase(); }); }