I have compiled some more practical Javascript form code snippets and shared them with you for your reference. The specific contents are as follows
1 Multiple window.onload methods
Since the onload method is automatically called after the page is loaded. Therefore, it is widely used, but the disadvantage is that only one method can be executed using onload. The following code snippet can ensure that multiple methods are executed during Onload:
function addLoadEvent(func){ var oldonload = window.onload; if(typeof window.onload != 'function'){ window.onload = func; }else{ window.onload = function(){ oldonload(); func(); } } }
2 Regular expression to remove spaces
str.replace(/^(\s|\u00A0)+|(\s|\u00A0)+$/g,"");
3 Use regular rules to filter Chinese
str.replace(/[\u4e00-\u9fa5]/g,"");
4 User copying and reproduction are prohibited
xxx.oncopy = function(){ return false; } xxx.onpaste = function(){ return false; }
5 Limit string length (differentiate between Chinese and English)
Main idea:
Requires 3 data: 1. Limit the length of input; 2. How long has been entered; 3. How many characters to intercept;
Since the interception method in JS does not distinguish between Chinese and English, so
"Hahaha".substr(0,2) ----> Haha
“haha”.substr(0,2) ---> ha
However, according to the encoding, one Chinese character should correspond to 2 characters, and one letter should correspond to one character.
Therefore, when counting the real length, it should be the length of the character + the number of Chinese characters
For example, we limit the input to 5 characters: then after entering "haha", you can only enter an h, and no more Chinese characters can be entered. The code reference is as follows, you can run it for further review.
<script type="text/javascript"> var strLen = (function(){//strlLength的功能相同,但是写法巨麻烦 return function(_str,_model){ _model = _model || "Ch"; //En模式下,中文算作1字符;Ch模式下,中文为2个字符 var _strLen = _str.length; //获取字符串长度 if(_strLen == 0){ return 0; }else{ var chinese = _str.match(/[\u4e00-\u9fa5]/g); //匹配中文 return _strLen + (chinese && _model == "Ch"?chinese.length:0); //为什么要&&? } } })(); var strLength = function(_str,_model){ _model = _model || "Ch"; //En模式下,中文算作1字符;Ch模式下,中文为2个字符 var _strLen = _str.length; //获取字符串长度 if(_strLen == 0){ return 0; }else{ var chinese = _str.match(/[\u4e00-\u9fa5]/g); //匹配中文 return _strLen + (chinese && _model == "Ch"?chinese.length:0); //为什么要&&? } } var funcRemainingCharacters = function(){ remainingCharacters = document.getElementById("remainingCharacters"); var clearRemainingCharacters = function(_this){ var _value = _this.value; var _valueLength = _value.length; var dataLength = _this.getAttribute("data-length"); var dataModel = _this.getAttribute("data-model"); var subLen = dataLength; if(dataModel == "Ch"){//仅当开启Ch后,才对重新计算截取的长度 _valueLength = strLength(_value,dataModel); var vv = _value.match(/[\u4e00-\u9fa5]/g); //当输入【哈哈】时,vv是["哈","哈"]数组 subLen = dataLength - (!vv?0:vv.length); } //_valueLength代表总共的字符长度,比如哈哈哈 为 6 //dataLength是我们定义的限制长度,比如 5 //subLen是计算的截取长度,当输入家具啊 if(_valueLength > dataLength){ _this.value = _value.substr(0,subLen); } } remainingCharacters.onfocus = function(){ clearRemainingCharacters(this); } remainingCharacters.onkeyup = function(){ clearRemainingCharacters(this); } remainingCharacters.onblur = function(){ clearRemainingCharacters(this); } } addLoadEvent(funcRemainingCharacters); </script>
All codes:
(支持中英文区分)限制字符串长度
<script type="text/javascript"> var strLen = (function(){//strlLength的功能相同,但是写法巨麻烦 return function(_str,_model){ _model = _model || "Ch"; //En模式下,中文算作1字符;Ch模式下,中文为2个字符 var _strLen = _str.length; //获取字符串长度 if(_strLen == 0){ return 0; }else{ var chinese = _str.match(/[\u4e00-\u9fa5]/g); //匹配中文 return _strLen + (chinese && _model == "Ch"?chinese.length:0); //为什么要&&? } } })(); var strLength = function(_str,_model){ _model = _model || "Ch"; //En模式下,中文算作1字符;Ch模式下,中文为2个字符 var _strLen = _str.length; //获取字符串长度 if(_strLen == 0){ return 0; }else{ var chinese = _str.match(/[\u4e00-\u9fa5]/g); //匹配中文 return _strLen + (chinese && _model == "Ch"?chinese.length:0); //为什么要&&? } } var funcRemainingCharacters = function(){ remainingCharacters = document.getElementById("remainingCharacters"); var clearRemainingCharacters = function(_this){ var _value = _this.value; var _valueLength = _value.length; var dataLength = _this.getAttribute("data-length"); var dataModel = _this.getAttribute("data-model"); var subLen = dataLength; if(dataModel == "Ch"){//仅当开启Ch后,才对重新计算截取的长度 _valueLength = strLength(_value,dataModel); var vv = _value.match(/[\u4e00-\u9fa5]/g); //当输入【哈哈】时,vv是["哈","哈"]数组 subLen = dataLength - (!vv?0:vv.length); } //_valueLength代表总共的字符长度,比如哈哈哈 为 6 //dataLength是我们定义的限制长度,比如 5 //subLen是计算的截取长度,当输入家具啊 if(_valueLength > dataLength){ _this.value = _value.substr(0,subLen); } } remainingCharacters.onfocus = function(){ clearRemainingCharacters(this); } remainingCharacters.onkeyup = function(){ clearRemainingCharacters(this); } remainingCharacters.onblur = function(){ clearRemainingCharacters(this); } } addLoadEvent(funcRemainingCharacters); </script>
6 Add CSS function
var setCSS = function(_this,cssOption){ if(!_this || _this.nodeType === 3 || _this.nodeType === 8 || !_this.style){ return; } for(var cs in cssOption){ _this.style[cs] = cssOption[cs]; } return _this; };
When using
setCSS(xxx,{ "position":"relative", "top":"0px" });
7 Adaptive text boxes
Copy scrollHeight to style.height
xxx.style.overflowY = "hidden"; xxx.onkeyup = function(){ xxx.style.height = xxx.scrollHeight+"px"; };
8 checkboxes to select, cancel and inverse
//下面html代码 <label class="checkbox-inline"> <input type="checkbox" name="actionSelects">读书 </label> <label class="checkbox-inline"> <input type="checkbox" name="actionSelects">跑步 </label> <label class="checkbox-inline"> <input type="checkbox" name="actionSelects">游戏 </label> <label class="checkbox-inline"> <input type="checkbox" name="actionSelects">游泳 </label> //下面是js代码 var targets = document.getElementsByName("actionSelects"); var targetsLen = targets.length; var i = 0; document.getElementById("allSelect").onclick = function(){ for(i=0;i<targetsLen;i++){ targets[i].checked = true; } } document.getElementById("cancelAllSelect").onclick = function(){ for(i=0;i<targetsLen;i++){ targets[i].checked = false; } } document.getElementById("_select").onclick = function(){ for(i=0;i<targetsLen;i++){ targets[i].checked = !targets[i].checked; } }
I hope this article will be helpful to everyone learning JavaScript programming.