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.

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version
Chinese version, very easy to use

Notepad++7.3.1
Easy-to-use and free code editor

Dreamweaver Mac version
Visual web development tools
