标签:监听器 右键 字典 alert value 初始化 本地 弱类型 define
<script> function fun1(a,b) { alert(a); } fun1(3); var fun2 = function (a,b) { alert(b); } document.write(fun1.length); //求任意个数的和 function add() { var sum = 0; for(var i = 0; i < arguments.length; i++){ sum += arguments[i]; } return sum; } </script>
<script> //取1-100之间的整数 var number = Math.random(); number = number * 100; var num = Math.floor(number); num += 1; document.write(num); </script>
<script> var reg = new RegExp("\\w{6,12}"); var reg2 = /\w{6,12}/; var username = "zhangsan"; var flag = reg.test(username); // document.write(flag); alert(flag); </script>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <!--内部JS 外部JS--> </head> <body> <img src="../HTMLandCSS/img/logo.jpg" id="p1"> <h1 id="title">悔创阿里杰克马</h1> <script> var e1 = document.getElementById("p1"); var title = document.getElementById("title"); alert("我要换内容了"); title.innerHTML = "还是京东好"; </script> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <img src="img/off.gif" id="p1"> <script> //1.获取图片对象 var p1 = document.getElementById("p1"); var flag = false; //代表灯是灭的 function f1() { if(flag){ //如果灯开着就灭掉 p1.src = "img/off.gif"; flag = false; }else { p1.src = "img/on.gif"; flag = true; } } p1.onclick = f1; </script> </body> </html>
<body> <input type="button" value="前往网页" id="tag_id1"> <input type="button" value="关闭网页" id="tag_id2"> <script> var tag1 = document.getElementById("tag_id1"); var tag2 = document.getElementById("tag_id2"); var obj; tag1.onclick = function () { obj = open("https://www.baidu.com"); }; tag2.onclick = function () { obj.close(); } </script> </body>
var id = setInterval("alert(‘boom‘)",3000); clearInterval(id);
<body> <img src="../img/off.gif" id="pp"> <script> var pid = document.getElementById("pp"); var num = 1; function switchPicture() { num ++; if(num % 2==0){ pid.src = "../img/off.gif"; }else if(num % 2 == 1){ pid.src = "../img/on.gif"; } } setInterval(switchPicture,2000); </script> </body>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> p{ text-align: center; } span{ color: red; } </style> </head> <body> <p> <span id="time">5</span>秒后跳转至首页 </p> <script> //通过定时器实现倒计时 var second = 5; var time = document.getElementById("time"); function showtime() { //时间如果小于0,则跳转 second --; if(second <= 0){ location.href = "https://www.baidu.com"; } time.innerHTML = second + ""; } setInterval(showtime,1000); </script> </body> </html>
<body> <a>点我试试</a> <input type="button" id="tag_button" value="给超链接设置网页"> <script> //获取这个a标签元素 var ele_a = document.getElementsByTagName("a")[0]; //拿到按钮 var ele_button = document.getElementById("tag_button"); //给按钮绑定事件 ele_button.onclick = function () { ele_a.setAttribute("href","https://www.baidu.com"); }; </script> </body>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Node</title> <style> div{ border: 1px solid red; } #div1{ height: 200px; width: 200px; } #div2{ height: 100px; width: 100px; } </style> </head> <body> <div id="div1"> <div id="div2">div2</div> div1 </div> 超链接功能: 1.可以被点击;有样式 2.点击后跳转到href指定的url 需求:保留功能1,去掉功能2 实现:href="javascript:void(0);" <a href="javascript:void(0);">删除子节点</a> <script> var ele_a = document.getElementsByTagName("a")[0]; //给超链接对象绑定点击事件 ele_a.onclick = function () { //获取div1这个父节点对象 var ele_div1 = document.getElementById("div1"); var ele_div2 = document.getElementById("div2"); ele_div1.removeChild(ele_div2); } </script> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>动态表格</title> <style> div{ text-align: center; margin: 50px; } table{ border: 1px solid; margin: auto; width: 500px; } td,th{ text-align: center; border: 1px solid; } </style> </head> <body> <div> <input type="text" id="id" placeholder="请输入编号"> <input type="text" id="name" placeholder="请输入姓名"> <input type="text" id="gender" placeholder="请输入性别"> <input type="button" value="添加" id="btn_add"> </div> <table> <caption>学生信息表</caption> <tr> <th>编号</th> <th>姓名</th> <th>性别</th> <th>操作</th> </tr> <tr> <td>1</td> <td>令狐冲</td> <td>男</td> <td><a href="javascript:void (0);" onclick="delTr(this);">删除</a></td> </tr> <tr> <td>2</td> <td>任我行</td> <td>男</td> <td><a href="javascript:void (0);" onclick="delTr(this);">删除</a></td> </tr> <tr> <td>3</td> <td>小龙女</td> <td>女</td> <td><a href="javascript:void (0);" onclick="delTr(this);">删除</a></td> </tr> </table> <script> /* 添加: 1.给添加按钮绑定单击事件 2.获取文本框的内容 3.创建td,去设置td的文本为文本框的内容 4.创建tr 5.将td添加到tr中 6.获取table,将tr添加到table 中 删除: 1.确定删除的是哪一个超链接 <a href="javascript:void (0);" onclick="del_tr(this)"> 2.怎么删除 */ //1.获取按钮 document.getElementById("btn_add").onclick = function () { //获取文本框的内容 var id = document.getElementById("id").value; var name = document.getElementById("name").value; var gender = document.getElementById("gender").value; //创建td,赋值td的标签体 var td_id = document.createElement("td"); //文本节点才能放入 var text_id = document.createTextNode(id); td_id.appendChild(text_id); var td_name = document.createElement("td"); var text_name = document.createTextNode(name); td_name.appendChild(text_name); var td_gender = document.createElement("td"); var text_gender = document.createTextNode(gender); td_gender.appendChild(text_gender); //a标签的td var td_a = document.createElement("td"); var a = document.createElement("a"); var text_a = document.createTextNode("删除"); a.appendChild(text_a); a.setAttribute("href","javascript:void(0);"); a.setAttribute("onclick","delTr(this);"); td_a.appendChild(a); //创建tr var tr = document.createElement("tr"); //添加td到tr中 tr.appendChild(td_id); tr.appendChild(td_name); tr.appendChild(td_gender); tr.appendChild(td_a); //获取table var table = document.getElementsByTagName("table")[0]; table.appendChild(tr); //删除方法 function delTr(obj) { var table = obj.parentNode.parentNode.parentNode; //获取tr对象 var tr = obj.parentNode.parentNode; table.removeChild(tr); alert(obj); } } </script> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>动态表格</title> <style> div{ text-align: center; margin: 50px; } table{ border: 1px solid; margin: auto; width: 500px; } td,th{ text-align: center; border: 1px solid; } </style> </head> <body> <div> <input type="text" id="id" placeholder="请输入编号"> <input type="text" id="name" placeholder="请输入姓名"> <input type="text" id="gender" placeholder="请输入性别"> <input type="button" value="添加" id="btn_add"> </div> <table> <caption>学生信息表</caption> <tr> <th>编号</th> <th>姓名</th> <th>性别</th> <th>操作</th> </tr> <tr> <td>1</td> <td>令狐冲</td> <td>男</td> <td><a href="javascript:void (0);" onclick="delTr(this);">删除</a></td> </tr> <tr> <td>2</td> <td>任我行</td> <td>男</td> <td><a href="javascript:void (0);" onclick="delTr(this);">删除</a></td> </tr> <tr> <td>3</td> <td>小龙女</td> <td>女</td> <td><a href="javascript:void (0);" onclick="delTr(this);">删除</a></td> </tr> </table> <script> /* 添加: 1.给添加按钮绑定单击事件 2.获取文本框的内容 3.创建td,去设置td的文本为文本框的内容 4.创建tr 5.将td添加到tr中 6.获取table,将tr添加到table 中 删除: 1.确定删除的是哪一个超链接 <a href="javascript:void (0);" onclick="del_tr(this)"> 2.怎么删除 */ //1.获取按钮 document.getElementById("btn_add").onclick = function () { //获取文本框的内容 var id = document.getElementById("id").value; var name = document.getElementById("name").value; var gender = document.getElementById("gender").value; var ele_table = document.getElementsByTagName("table")[0]; //向table里添加 ele_table.innerHTML += "<tr>\n" + " <td>"+id+"</td>\n" + " <td>"+name+"</td>\n" + " <td>"+gender+"</td>\n" + " <td><a href=\"javascript:void (0);\" onclick=\"delTr(this);\">删除</a></td>\n" + " </tr>"; //删除方法 function delTr(obj) { var table = obj.parentNode.parentNode.parentNode; //获取tr对象 var tr = obj.parentNode.parentNode; table.removeChild(tr); alert(obj); } } </script> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div id="div1"> div </div> <script> var ele_div1 = document.getElementById("div1"); ele_div1.onclick = function () { ele_div1.style.border = "1px solid red"; //font-size --> fontSize } </script> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .d1{ border: 1px solid red; } </style> </head> <body> <div id="div1"> div </div> <script> var ele_div1 = document.getElementById("div1"); ele_div1.onclick = function () { ele_div1.className = "d1"; } </script> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script> window.onload = function(){ var ele_1 = document.getElementById("tag1"); ele_1.onmousedown = function (event) { if(event.button==0){ alert("左键被按下了"); } } } </script> </head> <body> <input type="text" id="tag1"> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script> window.onload = function(){ var ele_1 = document.getElementById("tag1"); ele_1.onkeydown = function (event) { if(event.keyCode == 13){ alert("提交表单 、"); } } } </script> </head> <body> <input type="text" id="tag1"> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script> function checkForm() { //校验用户名格式是否正确 var flag = true; return flag; } </script> </head> <body> <form action="#" onsubmit="return checkForm();"> <input type="text" id="tag1" name="username"> <input type="submit" value="提交"> </form> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> table{ border: 1px solid; width: 500px; margin-left: 30%; } td,th{ text-align: center; border: 1px solid; } div{ margin-top: 10px; margin-left: 30%; } .over{ background-color: deeppink; } .out{ background-color: white; } </style> <script> /* 分析: 1.全选: *获取所有的checkbox *遍历cb,设置每一个cb的状态为选中 checked属性 */ //1.在页面加载完后绑定事件 window.onload = function () { //2.给全选按钮绑定单击事件 document.getElementById("selectAll").onclick = function () { //全选功能,获取所有checkbox var cbs = document.getElementsByName("cb"); //遍历 for(var i = 0;1 < cbs.length; i++){ //设置每一个checked属性为true cbs[i].checked = true; } } //3.给给不选按钮绑定单击事件 document.getElementById("unSelectAll").onclick = function () { //全不选功能,获取所有checkbox var cbs = document.getElementsByName("cb"); //遍历 for(var i = 0;1 < cbs.length; i++){ //设置每一个checked属性为false cbs[i].checked = false; } } //反选事件 document.getElementById("selectRev").onclick = function () { //全选功能,获取所有checkbox var cbs = document.getElementsByName("cb"); //遍历 for(var i = 0;1 < cbs.length; i++){ cbs[i].checked = !cbs[i].checked; } } //第一个cb点击 document.getElementById("first_cb").onclick = function () { //获取所有checkbox var cbs = document.getElementsByName("cb"); //遍历 for(var i = 0;1 < cbs.length; i++){ //设置每一个的状态和第一个的一样,this是onclick绑定的第一个cb cbs[i].checked = this.checked; } } //给所有的tr绑定一个移到元素之上和移除事件 var trs = document.getElementsByTagName("tr"); //遍历 for(var i = 0; i < trs.length; i++ ){ //移到元素之上 trs[i].onmouseover = function () { this.className = "over"; }; //移出元素 trs[i].onmouseout = function () { this.className = "out"; } } } </script> </head> <body> <table> <caption>学生信息表</caption> <tr> <th><input type="checkbox" name="cb" id="first_cb"></th> <td>编号</td> <td>姓名</td> <td>性别</td> <td>操作</td> </tr> <tr> <th><input type="checkbox" name="cb"></th> <td>1</td> <td>令狐冲</td> <td>男</td> <td><a href="javascript:void(0);">删除</a></td> </tr> <tr> <th><input type="checkbox" name="cb"></th> <td>2</td> <td>任我行</td> <td>男</td> <td><a href="javascript:void(0);">删除</a></td> </tr> <tr> <th><input type="checkbox" name="cb"></th> <td>3</td> <td>岳不群</td> <td>?</td> <td><a href="javascript:void(0);">删除</a></td> </tr> </table> <div> <input type="button" id="selectAll" value="全选"> <input type="button" id="unSelectAll" value="全不选"> <input type="button" id="selectRev" value="反选"> </div> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>注册页面</title> <style> body{ background: url("../../HTMLandCSS/img/register_bg.png") no-repeat center; } /*让所有元素都挨近 上下两行字*/ *{ margin: 0px; padding: 0px; } /*给最外层框框定义*/ .register_layout{ width: 900px; height: 500px; border: 8px solid #EEEEEE; background-color: white; /*让div水平居中*/ margin: auto; margin-top: 25px; } .rg_left{ /*border: 1px solid red;*/ float: left; margin: 15px; } .rg_center{ /*border: 1px solid red;*/ float: left; width: 450px; margin-top: 20px; margin-left: 30px; } .rg_right{ /*border: 1px solid red;*/ float: right; margin: 10px; } /*选择左边div下的第一个p标签*/ .rg_left > p:first-child{ color:#FFD026; font-size:20px; } /*选择左边div下的最后一个p标签*/ .rg_left > p:last-child{ color:#A6A6A6; font-size: 20px; } .rg_right p a { color: palevioletred; } /*表格中所有左侧的元素*/ .td_left{ width: 100px; height: 45px; text-align: right; } .td_right{ padding-left: 50px; } #username,#password,#email,#name,#tel,#checkcode,#birthday{ width: 160px; height: 26px; border: 1px solid #A6A6A6; /*设置边框圆角*/ border-radius: 5px; padding-left: 10px; } #checkcode{ width: 100px; } #img_check{ height: 32px; /*设置垂直居中*/ vertical-align: middle; } #sub{ width: 120px; height: 40px; background-color: #FFD026; border: 1px solid #FFD026; margin-left: 80px; } .error{ color: red; } #td_sub{ padding-left: 130px; } </style> <script> /* 分析: 1.给表单绑定onsubmit事件,监听器中判断每一个方法的校验结果 如果都为true,则监听器方法返回true 如果有一个为false,则监听器方法方法返回false 2.定义一些方法,分别校验各个表单项 3.给各个表单项绑定onblur离焦事件 */ window.onload = function () { //1.给表单绑定onsubmit事件 document.getElementById("form").onsubmit = function () { //调用用户名校验方法、密码校验方法 checkUserName checkPassword return checkUserName() && checkPassword(); } //给用户名和密码框分别绑定离焦事件 注意:只要函数对象!!!不要方法执行 document.getElementById("username").onblur = checkUserName; document.getElementById("password").onblur = checkPassword; }; //校验用户名 function checkUserName() { //1.获取用户名的值 var username = document.getElementById("username").value; //2.定义正则表达式 var reg_username = /^\w{6,12}$/; //3.判断值是否符合正则的规则 var flag = reg_username.test(username); //4.提示信息 //通过span标签来添加后面的图片或者文字提示 var s_username = document.getElementById("s_username"); if(flag){ //提示绿色对勾 s_username.innerHTML = "<img width=‘35px‘ height=‘25px‘ src=‘../img/on.gif‘/>" }else { //提示红色的用户名有误 s_username.innerHTML = "用户名格式有误"; } } //校验密码 function checkPassword() { //1.获取用户名的值 var password = document.getElementById("password").value; //2.定义正则表达式 var reg_password = /^\w{6,12}$/; //3.判断值是否符合正则的规则 var flag = reg_password.test(password); //4.提示信息 //通过span标签来添加后面的图片或者文字提示 var s_password = document.getElementById("s_password"); if(flag){ //提示绿色对勾 s_password.innerHTML = "<img width=‘35px‘ height=‘25px‘ src=‘../img/off.gif‘/>" }else { //提示红色的用户名有误 s_password.innerHTML = "密码格式有误"; } } </script> </head> <body> <div class="register_layout"> <div class="rg_left"> <p>新用户注册</p> <p>USER REGISTER</p> </div> <div class="rg_center"> <div class="rg_form"> <!--中间的表格--> <form action="#" method="get" id="form"> <table> <tr> <td class="td_left"><label for="username">用户名</label></td> <td class="td_right"><input type="text" name="username" id="username" placeholder="请输入用户名"> <span id="s_username" class="error"></span> </td> </tr> <tr> <td class="td_left"><label for="password">密码</label></td> <td class="td_right"><input type="password" name="password" id="password" placeholder="请输入密码"> <span id="s_password" class="error"></span> </td> </tr> <tr> <td class="td_left"><label for="email">email</label></td> <td class="td_right"><input type="email" name="email" id="email" placeholder="请输入email"></td> </tr> <tr> <td class="td_left"><label for="name">姓名</label></td> <td class="td_right"><input type="text" name="name" id="name" placeholder="请输入姓名"></td> <tr> <td class="td_left"><label for="tel">手机号</label></td> <td class="td_right"><input type="text" name="tel" id="tel" placeholder="请输入手机号"></td> </tr> <tr> <td class="td_left"><label>性别</label></td> <td class="td_right"> <input type="radio" name="gender" value="male">男 <input type="radio" name="gender" value="female">女 </td> </tr> <tr> <td class="td_left"><label for="birthday">出生日期</label></td> <td class="td_right"><input type="date" name="birthday" id="birthday" placeholder="请输入生日"></td> </tr> <tr> <td class="td_left"><label for="checkcode">验证码</label></td> <td class="td_right"><input type="text" name="checkcode" id="checkcode" placeholder="请输入验证码"><img src="../../HTMLandCSS/img/verify_code.jpg" id="img_check"></td> </tr> <tr> <td colspan="2" id="td_sub"><input type="submit" value="注册" id="sub"></td> </tr> </table> </form> </div> </div> <div class="rg_right"> <p>已有账号?<a href="#">立即登录</a></p> </div> </div> </body> </html>
标签:监听器 右键 字典 alert value 初始化 本地 弱类型 define
原文地址:https://www.cnblogs.com/caixiaowu/p/13156338.html