事件:
相当于监听器之类的:一种能被JavaScript侦测到,网页中每个元素都可以产生某些可以触发Javascript函数的事件;
属性:onclick 当用户点击某个事物的句柄
属性:oncontextmenu 当用户点击鼠标右键打开上下文菜单时触发
属性:ondblclicl 当用户双击某个对象调用事件句柄
属性: onmousedown 鼠标按钮按下
属性: onmouseleave 当鼠标指针移除元素时触发
属性: onmousout 鼠标从元素上移开
属性 :onmousemove 鼠标被移动
属性: onmouseup 鼠标按键被送开
-
- "change one" onclick="demo01()" onmouseover="demo02()">这是一个惊喜和子
-
-
-
- "change two" onmouseover="demo02()" onmouseout="demo03()">开盖有惊喜!
-
-
- "search" name="search" onkeyup="demo04(event)">
-
- function demo01(){
- alert("Surprise~~~");
- for(var i = 0; i <= 10; i++){
- console.log(i);
- }
- }
-
- function demo02(){
- console.log("恭喜您打开了神秘盒子,但是没有中奖!");
- }
-
- function demo03(){
- console.log("你快回来~~~~~");
- }
-
- function demo04(event){
- console.log(event.keyCode);
- if(event.keyCode == 32){
- window.location.href = "https://www.vip.com/"
- }
- }
属性:onkeydown 某个键盘被按下
属性:onkeypress莫个键盘按键被按下并松开
属性:onkeyup摸个键盘被松开
-
- "text" onchange="demo01()">
-
-
- "text" onblur="demo02()">
-
-
- "text" onfocus="demo03()">
- function demo01(){
- console.log("触发了onchange事件");
- }
-
- function demo02(){
- console.log("触发了onblur事件");
- }
-
- function demo03(){
- console.log("触发了onfocus事件");
- }
属性 onblur 元素失去焦点时触发
属性 onchange 该事件在表单元素的内容改变时触发( ,
- 'utf-8'>
- 'X-UA-Compatible' content='IE=edge'>
-
Page Title - 'viewport' content='width=device-width, initial-scale=1'>
-
- *{
- margin: 0;
- padding: 0;
- }
-
- div.login{
- width: 40%;
- height: 400px;
- background-color: aqua;
- margin: 50px auto;
- padding-top: 20px;
- background-image: url(../img/宇航员.gif);
- color: white;
- border-radius: 10px;
- }
-
- div.login h1{
- text-align: center;
- }
-
- form{
- width: 80%;
- height: 80%;
- margin: 30px auto;
- }
-
- div.userinput{
- width: 80%;
- height: 20%;
- margin: 40px auto;
- }
-
- div.userinput label{
- font-size: 20px;
- }
- div.userinput input{
- width: 70%;
- height:35px;
- margin-left: 20px;
- border-radius: 10px;
- outline: none;
- }
-
- div.buttonStyle{
- width: 80%;
- height: 20%;
- margin: auto;
- }
-
- input[type="submit"]{
- width: 100%;
- height: 50px;
- border-radius: 10px;
- background-color: gainsboro;
- border: none;
- }
-
- div#model{
- width: 40%;
- height: 420px;
- background-color: rgba(0, 0, 0, 0.5);
- border-radius: 10px;
- position: absolute;
- top: 0;
- left: 460px;
- }
-
- span#modelText{
- color: red;
- font-size: 30px;
- text-align: center;
- line-height: 420px;
- font-weight: 600;
- }
-
- span#hide{
- color: yellow;
- font-size: 30px;
- position: absolute;
- top: 10px;
- right: 10px;
- }
- "position: relative;">
- "login">
-
霍格沃兹入口
-
- "userinput">
-
- "text" name="username" id="username">
-
- "userinput">
-
- "password" name="userpwd" id="userpwd">
-
- "buttonStyle">
- "submit">
-
-
-
-
-
- "model">
- "modelText">
- "hide" onclick="hide()">X
-
-
- // 进入页面时,获取模态框节点并隐藏
- var model = document.getElementById("model");
- model.style = "display:none";
- // 获取模态框中文子节点
- var modelText = document.getElementById("modelText");
-
- function validity(){
- // 获取账号节点
- var username = document.getElementById("username");
- // 获取密码节点
- var userpwd = document.getElementById("userpwd");
- // 姓名正则
- var nameReg = /^([\u4e00-\u9fa5]{1,20}|[a-zA-Z\.\s]{1,20})$/;
- // 密码正则
- var pwdReg = /^[0-9a-zA-Z]{6,12}$/;
-
- // 验证账号是否为空
- if(username.value.length == 0){
- modelBlock();
- modelText.innerHTML = "账号不能为空";
- return false;
- }
-
- // 验证账号格式
- if(!nameReg.test(username.value)){
- modelBlock();
- modelText.innerHTML = "账号格式有误";
- return false;
- }
-
- // 验证密码是否为空
- if(userpwd.value.length == 0){
- modelBlock();
- modelText.innerHTML = "密码不能为空";
- return false;
- }
-
- // 验证密码格式
- if(!pwdReg.test(userpwd.value)){
- modelBlock();
- modelText.innerHTML = "密码格式有误";
- return false;
- }
- }
-
- function modelBlock(){
- // 显示模态框
- model.style = "display:block";
- }
-
- function hide(){
- // 关闭模态框
- model.style = "display:none";
- }
-
属性:onblur元素失去焦点
属性:onfocus元素获得焦点
属性:onchange域的内容被改变
加载事件
onload 一张页面或一副图像完成加载