工程项目中的常用功能,属于工具类,一般放到utils.js

获取getUrlParam
function getUrlParam(name) {
var reg=new RegExp(`(^|&)${name}=([^&]*)(&|$)`);
var r=window.location.href.match(/\?[^\?]*$/)[0].slice(1).match(reg);
if(r) return +unescape(r[2]); //解码字符串
return null;
}


手动实现bind函数,已经考虑了函数柯里化及作为构造函数时的注意点
Function.prototype.bind = function(context){
if (typeof this !== "function") {
throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
};
var args = Array.prototype.slice.call(arguments, 1),
F = function(){},
self = this,
bound = function(){
var innerArgs = Array.prototype.slice.call(arguments);
var finalArgs = args.concat(innerArgs);
return self.apply((this instanceof F ? this : context), finalArgs);
};

F.prototype = self.prototype;
bound.prototype = new F();
return bound;
};

7.限制input框只能输入数字
onkeypress="return /[\d]/.test(String.fromCharCode(event.keyCode))"
@keyup="form.store_size=form.store_size.replace(/[^\d]/,'')"
扩展:Vue
<input v-model.number="numCheck" type="number" step="1" /> 干掉默认样式


8.禁用文本框的n多方法
1.属性 disabled=true;
2.readonly=true;
3.onfocus=this.blur(); //即获得焦点时失去焦点
4.input.maxLength=0;
5.onkeypress(onkeydown,)=function(){return false;} //onkeyup并不起作用,原因见下文
限制input输入类型(多种方法实现)

来自 <https://www.cnblogs.com/eaysun/p/5490603.html>


9.禁用input框的缓存输入
Input.autocomplete='off' ('on'); //或者<form action='#' autocomplete='off'></form>

猜你喜欢

转载自www.cnblogs.com/ajaxkong/p/11687184.html