常用工具方法整理
2017-03-01 15:20
288 查看
本文主要整理收录在项目开发中,遇到的工具方法
生成唯一的ID
export function genUid(){ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8); return v.toString(16); }); }
获取Url上的参数,以{}返回
export function getQueryString() { let hash = location.hash; let queryObject = {}; let markIndex = hash.indexOf("?"); // contain query string in fragments if (markIndex > -1) { // build query object let queryString = hash.slice(markIndex + 1); let queryArray = queryString.split('&'); for (let i = 0; i < queryArray.length; i++) { let queryPair = queryArray[i].split('='); if (queryPair[0] !== '_k') { queryObject[queryPair[0]] = decodeURIComponent(queryPair[1]); } } } return queryObject; }
获取指定cookie
function getCookie(name) { var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)"); if (arr = document.cookie.match(reg)) return unescape(arr[2]); return null; }
让插件兼容commonjs AMD CMD 和原生JS
if (typeof module !== 'undefined' && typeof exports === 'object') { module.exports = beisenWebim; } else if (typeof define === 'function' && (define.amd || define.cmd)) { define(function () { return beisenWebim; }); } else { this.beisenWebim = beisenWebim; } }).call(function () { return this || (typeof window !== 'undefined' ? window : global); });
异常捕获函数
let errorObject = { value: null } function tryCatch(fn, ctx) { try { return fn.apply(ctx) } catch (e) { errorObject.value = e return errorObject } } //使用 var a=function(){ console.log(r) } let result = tryCatch(a,null) if(result == errorObject) console.log(errorObject.value) else{//do something else}
获取光标在textarea的位置坐标
// The properties that we copy into a mirrored div. // Note that some browsers, such as Firefox, // do not concatenate properties, i.e. padding-top, bottom etc. -> padding, // so we have to do every single property specifically. var properties = [ 'direction', // RTL support 'boxSizing', 'width', // on Chrome and IE, exclude the scrollbar, so the mirror div wraps exactly as the textarea does 'height', 'overflowX', 'overflowY', // copy the scrollbar for IE 'borderTopWidth', 'borderRightWidth', 'borderBottomWidth', 'borderLeftWidth', 'borderStyle', 'paddingTop', 'paddingRight', 'paddingBottom', 'paddingLeft', // https://developer.mozilla.org/en-US/docs/Web/CSS/font 'fontStyle', 'fontVariant', 'fontWeight', 'fontStretch', 'fontSize', 'fontSizeAdjust', 'lineHeight', 'fontFamily', 'textAlign', 'textTransform', 'textIndent', 'textDecoration', // might not make a difference, but better be safe 'letterSpacing', 'wordSpacing', 'tabSize', 'MozTabSize' ]; var isBrowser = (typeof window !== 'undefined'); var isFirefox = (isBrowser && window.mozInnerScreenX != null); export default function getCaretCoordinates(element, position, options) { if(!isBrowser) { throw new Error('textarea-caret-position#getCaretCoordinates should only be called in a browser'); } var debug = options && options.debug || false; if (debug) { var el = document.querySelector('#input-textarea-caret-position-mirror-div'); if ( el ) { el.parentNode.removeChild(el); } } // mirrored div var div = document.createElement('div'); div.id = 'input-textarea-caret-position-mirror-div'; document.body.appendChild(div); var style = div.style; var computed = window.getComputedStyle? getComputedStyle(element) : element.currentStyle; // currentStyle for IE < 9 // default textarea styles style.whiteSpace = 'pre-wrap'; if (element.nodeName !== 'INPUT') style.wordWrap = 'break-word'; // only for textarea-s // position off-screen style.position = 'absolute'; // required to return coordinates properly if (!debug) style.visibility = 'hidden'; // not 'display: none' because we want rendering // transfer the element's properties to the div properties.forEach(function (prop) { style[prop] = computed[prop]; }); if (isFirefox) { // Firefox lies about the overflow property for textareas: https://bugzilla.mozilla.org/show_bug.cgi?id=984275 if (element.scrollHeight > parseInt(computed.height)) style.overflowY = 'scroll'; } else { style.overflow = 'hidden'; // for Chrome to not render a scrollbar; IE keeps overflowY = 'scroll' } div.textContent = element.value.substring(0, position); // the second special handling for input type="text" vs textarea: spaces need to be replaced with non-breaking spaces - http://stackoverflow.com/a/13402035/1269037 if (element.nodeName === 'INPUT') div.textContent = div.textContent.replace(/\s/g, '\u00a0'); var span = document.createElement('span'); // Wrapping must be replicated *exactly*, including when a long word gets // onto the next line, with whitespace at the end of the line before (#7). // The *only* reliable way to do that is to copy the *entire* rest of the // textarea's content into the <span> created at the caret position. // for inputs, just '.' would be enough, but why bother? span.textContent = element.value.substring(position) || '.'; // || because a completely empty faux span doesn't render at all div.appendChild(span); var coordinates = { top: span.offsetTop + parseInt(computed['borderTopWidth']), left: span.offsetLeft + parseInt(computed['borderLeftWidth']) }; if (debug) { span.style.backgroundColor = '#aaa'; } else { document.body.removeChild(div); } return coordinates; } //调用方式 let coordinates = getCaretCoordinates(e.target, e.target.selectionEnd);
相关文章推荐
- 码农常用工具,方法、技巧等收集整理
- JavaScript 深入学习及常用工具方法整理 ---- 01.浮点数
- linux系统常用工具命令方法整理
- 工作之余,整理了一下平时处理数据的一些常用的,简单的方法。希望对学习JAVA的朋友有点帮助
- 中国***常用的八种工具 及其防御方法
- linux整理笔记之十四:常用系统安全分析工具
- Ubuntu下常用小工具安装以及扩大磁盘分区方法
- SEO常用在线工具整理
- WDSsoft免费源代码,java加密解密编程常用工具包 JCT 1.0 (整理)
- Linux下的FTP工具:lftp常用的使用方法
- 推荐几个WEB中常用的工具方法
- jquery 常用方法整理
- 中国黑客常用的八种工具及其防御方法
- 整理出C#(.net)的常用函数和方法集
- 中国***常用的八种工具 及其防御方法
- 常用小工具整理——持续更新
- ASP.NET中常用的优化性能的方法(转贴,Icyer收集整理)
- 整理 .Net 2.0 下 WinForms中常用的方法(更新中...)
- Android下个人整理的一些工具方法
- 中国***常用的八种工具及其防御方法