1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script type="text/javascript"> /*window.onload= function(){ var bjnObj = document.getElementById("btn01"); bjnObj.onclick=function d (){ alert("合法"); } } window.onload= function(){ var btnObj = document.getElementById("btn02");//定义对象 // alert(btnObj); btnObj.onclick = function a (){//定义方法 alert("动态点击"); } }*/ // var patt =new RegExp("1");和下面的写法相同正则表达式 //var patt =/e/; // alert(patt) // var str = "abe1cd"; //alert(patt.test(str));
function onclickFun(){ var newObj =document.getElementById("newObj"); //alert(newObj.value); var usernameText =newObj.value; //验证文本是否合法 var patt =/^\w{5,12}$/;//5-12个字符串正则表达式 /*test()方法用于测试某个字符串是否匹配*/
var usenaemspanObj = document.getElementById("usenaemspan");{ //alert(usenaemspanObj.innerHTML);//innerHTML表示标签中的内容
usenaemspanObj.innerHTML="标签修改"//可读可写可赋值 }
if(patt.test(usernameText)){ alert("用户名合法"); usenaemspanObj.innerHTML="<img src=\"../images/123.png\" width=\"20\" height=\"20\">"//在此处需要转义 } else{ alert("用户名不合法"); usenaemspanObj.innerHTML="<img src=\"../images/123.png\" width=\"40\" height=\"40\">"//vsc在此处需要转义 } } function checkAll(){//同一个js写全才不报错
var hobbies = document.getElementsByName("hobby");//name返回元素集合 //alert(hobbies.length);返回个数 //alert(hobbies[0].value);//input cpp //集合中每个元素都是dom对象 有序 //checked="checked"选中状态 checked可读可选 for(var i = 0; i < hobbies.length ; i++){ hobbies[i].checked=true; } }//全选 function checkNO() { var hobbies = document.getElementsByName("hobby");//name返回元素集合 for(var i = 0; i < hobbies.length ; i++){ hobbies[i].checked=false; } }//全不选 function checkReverse(){ var hobbies = document.getElementsByName("hobby");//name返回元素集合 for(var i = 0; i < hobbies.length ; i++){ hobbies[i].checked = !hobbies[i].checked;//比if更容易判断 /*if(hobbies[i].checked==false) hobbies[i].checked=true; else hobbies[i].checked=false;*/ } }//反选 function checkAll1() { var hobbies =document.getElementsByTagName("input")//指定标签名查询并返回集合 for(var i = 0; i < hobbies.length ; i++){ hobbies[i].checked = !hobbies[i].checked;//比if更容易判断 /*if(hobbies[i].checked==false) hobbies[i].checked=true; else hobbies[i].checked=false;*/ } }//反选 //创建标签 window.onload=function(){ var divObj = document.createElement("div"); divObj.innerHTML ="白筱1231321"; document.body.appendChild=("divObj") } </script> <title>DOM模型</title> </head> <body> <a href="www.white-x.top">白筱</a> <h1 align=center>标题</h1> 用户名<input type="text" id="newObj" value=""> <span style="color: red;" id="usenaemspan"></span> <button onclick="onclickFun()">校验</button><br> <input type="checkbox" name="hobby" value="cpp">cpp <input type="checkbox" name="hobby" value="java">java <input type="checkbox" name="hobby" value="js">js <input type="checkbox" name="hobby" value="python">python <br> <button onclick="checkAll()">全选</button> <button onclick="checkNO()">全不选</button> <button onclick="checkReverse()">反选</button> <br> <input type="checkbox" value="cpp">1 <input type="checkbox" value="java">2 <input type="checkbox" value="js">3 <input type="checkbox" value="python">4 <button onclick="checkAll1()">反选</button> </body> </html>
|