1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
| /*获取网址参数*/ getURL(name){ var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)"); var r = decodeURI(window.location.search).substr(1).match(reg); if(r!=null) return r[2]; return null; }
/*获取全部url参数,并转换成json对象*/ getUrlAllParams (url) { var url = url ? url : window.location.href; var _pa = url.substring(url.indexOf('?') + 1), _arrS = _pa.split('&'), _rs = {}; for (var i = 0, _len = _arrS.length; i < _len; i++) { var pos = _arrS[i].indexOf('='); if (pos == -1) { continue; } var name = _arrS[i].substring(0, pos), value = window.decodeURIComponent(_arrS[i].substring(pos + 1)); _rs[name] = value; } return _rs; }
/*删除url指定参数,返回url*/ delParamsUrl(url, name){ var baseUrl = url.split('?')[0] + '?'; var query = url.split('?')[1]; if (query.indexOf(name)>-1) { var obj = {} var arr = query.split("&"); for (var i = 0; i < arr.length; i++) { arr[i] = arr[i].split("="); obj[arr[i][0]] = arr[i][1]; }; delete obj[name]; var url = baseUrl + JSON.stringify(obj).replace(/[\"\{\}]/g,"").replace(/\:/g,"=").replace(/\,/g,"&"); return url }else{ return url; } }
/*获取十六进制随机颜色*/ getRandomColor () { return '#' + (function(h) { return new Array(7 - h.length).join("0") + h; })((Math.random() * 0x1000000 << 0).toString(16)); }
/*图片加载*/ imgLoadAll(arr,callback){ var arrImg = []; for (var i = 0; i < arr.length; i++) { var img = new Image(); img.src = arr[i]; img.onload = function(){ arrImg.push(this); if (arrImg.length == arr.length) { callback && callback(); } } } }
/*音频加载*/ loadAudio(src, callback) { var audio = new Audio(src); audio.onloadedmetadata = callback; audio.src = src; }
/*DOM转字符串*/ domToStirng(htmlDOM){ var div= document.createElement("div"); div.appendChild(htmlDOM); return div.innerHTML }
/*字符串转DOM*/ stringToDom(htmlString){ var div= document.createElement("div"); div.innerHTML=htmlString; return div.children[0]; }
/** * 光标所在位置插入字符,并设置光标位置 * * @param {dom} 输入框 * @param {val} 插入的值 * @param {posLen} 光标位置处在 插入的值的哪个位置 */ setCursorPosition (dom,val,posLen) { var cursorPosition = 0; if(dom.selectionStart){ cursorPosition = dom.selectionStart; } this.insertAtCursor(dom,val); dom.focus(); console.log(posLen) dom.setSelectionRange(dom.value.length,cursorPosition + (posLen || val.length)); }
/*光标所在位置插入字符*/ insertAtCursor(dom, val) { if (document.selection){ dom.focus(); sel = document.selection.createRange(); sel.text = val; sel.select(); }else if (dom.selectionStart || dom.selectionStart == '0'){ let startPos = dom.selectionStart; let endPos = dom.selectionEnd; let restoreTop = dom.scrollTop; dom.value = dom.value.substring(0, startPos) + val + dom.value.substring(endPos, dom.value.length); if (restoreTop > 0){ dom.scrollTop = restoreTop; } dom.focus(); dom.selectionStart = startPos + val.length; dom.selectionEnd = startPos + val.length; } else { dom.value += val; dom.focus(); } }
|