js个人笔记简记

正则表达式

创建正则表达式的两种方法:

 

  1. Var reg = new RegExp(‘’)
  2. Var reg = / a/

后面三个参数g:全局匹配 i:不区分大小写 m:多行匹配 开头结尾有用

常见的字符串方法:

indexof() 匹配首个满足条件的索引值,没有返回-1。

search() 检索与正则表达式相匹配的值,有返回索引值,没有返回-1。

substring() 截取字符串

charAt() 截取某一个字符

split() 将字符串分割 生成数组

match()把所有匹配的值取出来 返回值数组

replace() 替换

表单验证方法:

聚焦 onfocus

失焦 onblur

内容改变时调用的方法 onchange

键盘按下 onkeydown

键盘抬起 onkeyup

正则表达式的方法:

test() 检索字符串中指定的值 返回t/f

exec() 检索字符串中返回查找结果中的第一个值 返回数组 没有返回null

lastIndex

在全局匹配模式下 可以指定要查找的字符串 执行多次匹配

每次匹配使用当前正则对象的lastIndex属性值作为在目标字符串中开始查找的起始位置

lastIndex 属性初始位置0  找到匹配项后 lastIndex的值被重置为匹配值的下一个字符的索引位置,用来标识执行匹配的开始查找位置

字符的级别

 

d 数字 D 非数字

w 字母数字下划线 W 非字母数字下划线

s 空 S 非空

^ 开头

$ 结尾

. 匹配除换行符外的

[a-z]小写字母a-z

[A-Z]大写字母a-z

[0-9]数字

[^]除了方括号里的字符

限定符

*重复零次或多次

+重复一次或多次

?重复零次或一次

{n,m}重复n-m次

 

Json

 

 

将字符串转换为js对象

第一种方法:eval()是js自带的转换json字符串为对象的方法,转换之前必须给字符串添加一个()以避免错误。

第二种方法:JSON.parse()

for in: for(变量 in 对象){

         执行的代码

}

Math:

         ceil向上取整

         floor向下取整

         round四舍五入

         min最小值

         max最大值

         random随机数(0-0.99)

Date:

 var date=new Date(); new出来一个新对象

 getFullYear();

getMonth()+1;

getDate();

getHours();

getMinutes();

getSeconds();

getTime();获取1970年1月1日8:00:00到现在的毫秒数 时间戳

setTime();将时间戳转换成时间

BOM知识点:

         加载事件 onload

         location.href 地址

         window.open打开窗口

         window.close关闭窗口

         back()后退

         history.forward() 历史 可以作前进使用

         定时器:setInterval(function(){},时间)

         清除定时器:clearInterval

         延时器:setTimeout

         清除延时器:clearTimeout

获得浏览器宽 window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;2

         获得盒子宽高:offsetWidth offsetHeight

substring(截取位置,截取长度)

indexof()获得字符所在索引值

事件

Tab切换:

第一种:

Var btns=document.querySelectorAll(“button”);

Var conts=document.querySelectorAll(“p”);

for(var i=0;i<btns.length;i++){

         btns[i].index=I;

         btns[i].onclick=function(){

                  document.querySelector(“.select”).className=””;

                  document.querySelector(“.show”).calssName=””;

                  this.calssName=”select”;

                  conts[this.index].className=”show”;

}

btns[i].onmouseenter=function(){

         this.onclick();

}

}

第二种:

Var btns=document.querySelectorAll(“button”);

Var conts=document.querySelectorAll(“p”);

for( var i=0;i<btns.length;i++){

         btns[i].onclick=function(){

                  for(i=0;i<btns.length;i++){

                          if(btns[i]==this){

                                   this.calssName=”select”;

                                   conts[i].calssName=”show”;

}

else{

         btns[i].calssName=””;

         conts[i].calssName=””;

}

}

}

         btns[i].onmouseenter=function(){

                  this.onclick();

}

}

阻止事件冒泡:stopPropagation()

阻止默认行为:preventDefault()

DOM

document.getElementById();

document.getElementsByClassName();

document.getElementsByTagName();

document.querySelector/All();

parentNode父节点                    

nextElementSibling下一个兄弟节点

previousElementSibling上一个兄弟节点

children();子节点

lastElementChild firstElementChild

document.createElement创建节点

appendChild();将元素添加到最后面

insertBefore(插入的子节点,参考的子节点)插入到任意位置

removeChild();移出节点

replaceChild();替换节点

cloneNode()克隆节点 ture false

getAttribute();读取属性

setAttribute();设置属性

innerText添加内容

innerHTML添加内容

className修改类名

数组

Var arr=[];

冒泡排序

 

Var arr=[2,3,5,6,4,1,8]

for(var j=0;j<arr.length-1;j++){

for(var i=0;i<arr.length-1-j;i++){

         if(arr[i]>arr[i+1]){

                  var c=arr[i];

                  arr[i]=arr[i+1];

                  arr[i+1]=c

}      

}

}

数组常用方法

 

push();添加元素到数组后面,并返回长度

shift();添加元素带数组前面,并返回长度

unshift();删除第一个元素,并返回该元素

pop();删除最后一个元素,并返回该元素

splice(开始位置,删除长度,替换内容)

concat合并数组

sort正序 reverse倒序

join将数组转换成字符串

原文地址:https://www.cnblogs.com/white55k/p/11839772.html