|-----js执行原理
客户端与服务端
pc请求,给服务端,服务器响应后,把js脚本下载到pc端,“脱离”了服务端,本机会执行脚本(js).除非服务端的js作了更新!
|----js组成
<script type="text/javascript" src="链接外部js|前端框架"></script>
镂空的菱形:
<head> <meta charset="utf-8" /> <title></title> <script> function abc(){ var rows=prompt("请入菱形的行数:",""); //转换整型 var row=parseInt(rows); while(row%2==0){ row= prompt("请入菱形的行数:",""); row=parseInt(row); } //打印上部份 var uprow=(row+1)/2; for(var i=1;i<=uprow;i++){ //占位 for(var j=1;j<=uprow-i;j++){ document.write(" "); } for(var k=1;k<=2*i-1;k++){ //document.write("*"); if(k==1||k==2*i-1){ document.write("*"); }else{ document.write(" "); } } //换行 document.write("<br/>"); } //打印下部份 var downrow=row-uprow; for(var i=downrow;i>=1;i--){ //占位 for(var j=1;j<=downrow-i+1;j++){ document.write(" "); } for(var k=1;k<=2*i-1;k++){ //document.write("*"); if(k==1||k==2*i-1){ document.write("*"); }else{ document.write(" "); } } //换行 document.write("<br/>"); } } </script> </head> |
return用法
<head> <meta charset="UTF-8"> <title></title> <script> function abc(email){ //1.@ if(email.indexOf("@")==-1){ alert("邮箱地址必须包含@"); return;//停在当前的位置 } //2.点与@ 位置 if(email.lastIndexOf(".")<email.indexOf("@")){ alert("邮箱中@必须在.之前!"); return; } alert("正确的电子邮箱:"+email); } </script> </head> <body> <input type="button" value="测试" onclick="abc(prompt('请输入电子邮箱:',''));"/> </body> |
|------1:document.getElementById(); 针对单个元素(节点)
|------2:document.getElementsByName();多个
|------3:document.getElementsByTagName();表单提交的时候,<input type="text"/>
|------4:混用
|-----history: 作用:前进和后退 前提条件是,看浏览器箭头能否点击!
|-----history.forward() ====>history.go(1)
|-----history.back() ===>history.go(-1)
<a href="javascript:history.go(-1);">返回</a>
|-----confirm(); 提示用户,确认?
var cfm=confirm("是否要删除此条数据?") //交互 true false
open()用法:
属性名称 | 说 明 |
height、width | 窗口文档显示区的高度、宽度。以像素计 |
left、top | 窗口的x坐标、y坐标。以像素计 |
toolbar=yes | no |1 | 0 | 是否显示浏览器的工具栏。黙认是yes |
scrollbars=yes | no |1 | 0 | 是否显示滚动条。黙认是yes |
location=yes | no |1 | 0 | 是否显示地址地段。黙认是yes |
status=yes | no |1 | 0 | 是否添加状态栏。黙认是yes |
menubar=yes | no |1 | 0 | 是否显示菜单栏。黙认是yes |
resizable=yes | no |1 | 0 | 窗口是否可调节尺寸。黙认是yes |
titlebar=yes | no |1 | 0 | 是否显示标题栏。黙认是yes |
fullscreen=yes | no |1 | 0 | 是否使用全屏模式显示浏览器。黙认是no。处于全屏模式的窗口必须同时处于剧院模式 |
|----基本格式
window.open("other.html","other","height=250px,width=300px,top=100px,left=100px,toolbar=yes,scrollbars=yes,resizable=yes");
|-----js 启动时间
让时间停止下来
cleartimeout();
js实现下拉列表框的循环方式
全选:
-js的内置(系统提供的)对象
Math对象 random() ceil() floor()
<script> function abc(){ //随机数 //var r=Math.random();//介于0~1之间 var r=Math.floor(Math.random()*10); //var r=Math.floor(21.123);//向下取整不变 //var r=Math.ceil(21.012);//向上取整加一个 document.getElementById("mydiv").innerHTML=r; } </script> |
Math+Array
function abc(){ var arr=new Array("小明","小红","红红","小小","大大","小王","jack","rose","老王","王王"); //随机数 var r=Math.floor(Math.random()*10); document.getElementById("mydiv").innerHTML=arr[r]; } |