Js經常使用函數和問題react
函數jquery
getCurKeyCode瀏覽器
=====================微信
獲得輸入的keyCodcookie
=====================e session
$(document).on("keyup", "input", function(evt){dom
console.log(evt.keyCode)函數
}) this
* 在微信中瀏覽器不支持keyCode,獲得的keyCode都等於0;spa
getCaretPosition
====================
獲得當前輸入位置
====================
function doGetCaretPosition (oField) {
// Initialize
var iCaretPos = 0;
// IE Support
if (document.selection) {
// Set focus on the element
oField.focus();
// To get cursor position, get empty selection range
var oSel = document.selection.createRange();
// Move selection start to 0 position
oSel.moveStart('character', -oField.value.length);
// The caret position is selection length
iCaretPos = oSel.text.length;
}
// Firefox support
else if (oField.selectionStart || oField.selectionStart == '0') {
iCaretPos = oField.selectionStart;
if (isNaN(iCaretPos)) {
iCaretPos = 0
}
}
return iCaretPos;
}
getOrSetCookie
function getOrSetCookie(){
var fn = $.fn.cookie;
if ($.hasOwnProperty("cookie")) {
fn = $.cookie;
}
if(arguments.length == 1) {
return fn(name);
}
fn(name, value, expire);
}
* 兼容jquery 或者 zepto-cookie的cookie處理
Q&A