JS常用事件及触发方法
发布日期:2021-05-07 09:36:54 浏览次数:22 分类:原创文章

本文共 2803 字,大约阅读时间需要 9 分钟。

onclick点击事件

 <!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" />    <title>Document</title>  </head>  <body>    <button id="but">点击</button>  </body></html><script>  let but = document.getElementById("but");  but.addEventListener('click',function(){       alert("哈喽")  })</script>

onmouseover鼠标移入 和 onmouseout 鼠标移出事件

onmouseover 和 onmouseout 事件可用于在用户的鼠标移至 HTML 元素上方或移出元素时触发函数

<!DOCTYPE html><html><body><div onmouseover="mOver(this)" onmouseout="mOut(this)" style="background-color:green;width:120px;height:20px;padding:40px;color:#ffffff;">把鼠标移到上面</div><script>function mOver(obj){   obj.innerHTML="谢谢"}function mOut(obj){   obj.innerHTML="把鼠标移到上面"}</script></body></html>

onmousedown鼠标按下事件、onmouseup鼠标抬起事件

onmousedown, onmouseup 以及 onclick 构成了鼠标点击事件的所有部分。首先当点击鼠标按钮时,会触发 onmousedown 事件,当释放鼠标按钮时,会触发 onmouseup 事件,最后,当完成鼠标点击时,会触发 onclick 事件。

<!DOCTYPE html><html><body><div onmousedown="mDown(this)" onmouseup="mUp(this)" style="background-color:green;color:#ffffff;width:90px;height:20px;padding:40px;font-size:12px;">请点击这里</div><script>function mDown(obj){   obj.style.backgroundColor="#1ec5e5";obj.innerHTML="请释放鼠标按钮"}function mUp(obj){   obj.style.backgroundColor="green";obj.innerHTML="请按下鼠标按钮"}</script></body></html>

onfocus获取焦点事件

<!DOCTYPE html><html><head><script>function myFunction(x){   x.style.background="green";}</script></head><body>请输入英文字符:<input type="text" onfocus="myFunction(this)"><p>当输入字段获得焦点时,会触发改变背景颜色的函数。</p></body></html>

onchange 事件

onchange 事件常结合对输入字段的验证来使用

<!DOCTYPE html><html><head><script>function myFunction(){   var x=document.getElementById("fname");x.value=x.value.toUpperCase();}</script></head><body>请输入英文字符:<input type="text" id="fname" onchange="myFunction()"><p>当您离开输入字段时,会触发将输入文本转换为大写的函数。</p></body></html>

onload和onunload 事件

onload事件:指的是页面加载事件。
onunload事件:指的是离开页面事件

<!DOCTYPE html><html><body onload="test()"><script>function test(){   if (navigator.cookieEnabled==true)    {       alert("已启用 cookie")    }else    {       alert("未启用 cookie")    }}</script><p>提示框会告诉你,浏览器是否已启用 cookie。</p></body></html>

onkeydown键盘按下事件

<!DOCTYPE html><html lang="en">  <head>    <meta charset="UTF-8" />    <title>Document</title>  </head>  <body>    <input type="text" id="inp" />  </body></html><script>  let inp = document.getElementById("inp");  inp.addEventListener("keydown", function (e) {       if (e.keyCode == 13) {         alert("你好");    }  });</script>

onkeypress

事件会在键盘按键被按下并释放一个键时发生

<!DOCTYPE html><html><head><meta charset="utf-8"><title></title><script>function myFunction(){   	alert("你在输入框内按下一个按键");}</script></head><body><p>当用户在输入框内按下一个按键时函数被触发</p><input type="text" onkeypress="myFunction()"></body></html>
上一篇:vue.js常用指令及用法
下一篇:从输入url到页面加载完成发生了什么

发表评论

最新留言

做的很好,不错不错
[***.243.131.199]2025年03月20日 08时17分39秒