1.JS的五种基本数据类型:字符串、数值、布尔、null、underfined。
2.在JS中,字符串、数值、布尔三种数据类型,有其属性和方法;
3.字符串的三种常用方法[.indexof()、.substring()、.split()]和一种常用属性[.length];
例子如下:
/*提取字符串中列表的每一项*/ var aa="This is a list:red,blue,white,black."; var start=aa.indexOf(":"); /* 找到:在的索引*/ var end=aa.indexOf("."); /* 找到字符串最后的.的索引*/ var str=aa.substring(start+1,end); /*substring("起始索引","结束索引");如果没有结束索引,默认截取到末位置*/ var color=str.split(","); /*split(",");按照,进行分割,分割之后是数组*/ for(var i=0;i<color.length;i++) { alert(color[i]); }
4.向字符串中插入转义字符
例如:向已知字符串插入版权符号 用\u00A9是插入版权符号
var aa="This is a \u00A9 list.";
alert(aa);
常用转义字符:\'单引号 \"双引号 \\反斜杠 \b退格 \f换页符 \n换行 \r回车 \t水平制表符
5.字符串的替换,replace("旧字符","新字符或者生成新字符的函数");
6.addEventListener("click/mouseover等事件","执行函数","true/false") 方法用于向指定元素添加事件句柄,即给元素添加绑定事件。true - 事件句柄在捕获阶段执行.false- false- 默认。事件句柄在冒泡阶段执行。
<script> var x = document.getElementById("myBtn"); x.addEventListener("click", myFunction); x.addEventListener("click", someOtherFunction); function myFunction() { alert ("Hello World!") } function someOtherFunction() { alert ("该函数也将被执行!") } </script>
7.鼠标跟随事件
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>无标题文档</title> <style> body{margin:0; padding:0} #to_top{width:30px; height:40px; padding:20px; font:14px/20px arial; text-align:center; background:#06c; position:absolute; cursor:pointer; color:#fff} </style> </head> <body style="height:1000px;"> <div id="to_top">鼠标跟随</div><!--跟随鼠标的div,一定要设置position,并且position一定是absolute--> </body> </html> <script> window.onload = function(){ var oTop = document.getElementById("to_top"); document.onmousemove = function(evt){ var oEvent = evt || window.event; var scrollleft = document.documentElement.scrollLeft || document.body.scrollLeft; var scrolltop = document.documentElement.scrollTop || document.body.scrollTop; oTop.style.left = oEvent.clientX + scrollleft +10 +"px"; oTop.style.top = oEvent.clientY + scrolltop + 10 + "px"; } } </script>