当前位置: 首页 > news >正文

深圳市政府网站建设 网站管理太原网站制作优化seo公司

深圳市政府网站建设 网站管理,太原网站制作优化seo公司,苏州优化收费,建设银行网站-个人业务HTML 事件是发生在 HTML 元素上的事情。 当在 HTML 页面中使用 JavaScript 时, JavaScript 可以触发这些事件。 Html事件 HTML 事件可以是浏览器行为,也可以是用户行为。 以下是 HTML 事件的实例: HTML 页面完成加载HTML input 字段改变…

HTML 事件是发生在 HTML 元素上的事情。

当在 HTML 页面中使用 JavaScript 时, JavaScript 可以触发这些事件。

Html事件

HTML 事件可以是浏览器行为,也可以是用户行为。

以下是 HTML 事件的实例:

  • HTML 页面完成加载
  • HTML input 字段改变时
  • HTML 按钮被点击

通常,当事件发生时,你可以做些事情。

在事件触发时 JavaScript 可以执行一些代码。

事件绑定的方式

方式1:直接把事件绑定在HTML元素上

<button onclick="show('hello')">一个按钮</button>function show(str) {alert("点击事件执行了"+str);}

方式2:使用代码来绑定事件

<body><ul><li class="aa">01</li><li class="aa">02</li><li class="aa">03</li><li class="aa">04</li><li class="aa">05</li></ul></body><script>let a = document.getElementsByClassName("aa");a[0].onclick = function() {alert("第一个执行了");}</script>

方式3:绑定事件

 <input type="button" value="我的按钮" id="btn">let btn = document.getElementById("btn");var fun=function() {alert("按钮被点击了");}btn.addEventListener('click', fun);

事件解绑

<button id="btn">一个按钮</button>var b=function() {alert("222222")
}
btn.addEventListener("click",b)btn.removeEventListener("click",b);

常见的Html事件

焦点事件

针对表单项

onfocus  获取焦点

onblur     失去焦点

oninput      表单中内容发生变化时就触发

<body><input type="text" name="username" value="请输入用户名" onfocus="show1()"  oninput="show3()"><span id="sp"></span></body><script>function show1() {document.getElementsByName("username")[0].value=""; //获取焦点时 输入框变为空}function show3() {console.log("边输入边校验");let input = document.getElementsByName("username")[0];let content = input.value;let regx=/^[a-z]{6,16}$/i;let flag = regx.test(content);let sp = document.getElementById("sp");if(flag){input.style.border = "2px solid green";sp.innerHTML = "<b style='color: green'>格式正确</b>";}else {input.style.border = "2px solid red";sp.innerHTML = "<b style='color: red'>格式错误</b>";}}</script>

键盘事件

* onkeydown 某个键盘按键被按下。不区分键码的大小写

* onkeypress 某个键盘按键被按下并松开。区分键码的大小写 a 97  A 65

* onkeyup 某个键盘按键被松开。

<body><input type="text" onkeypress="show()"></body><script>function show() {let code = event.keyCode;if(code===97||code===65){console.log("往前跑");}console.log("按键按下了:"+code)}</script>

鼠标事件

onmousedown  鼠标按下

onmouseup   鼠标松开

onmouseover    鼠标移上

onmouseout    鼠标移开

onmousemove   鼠标移动

<body><button id="btn" onmousedown="down()" onmouseup="up()" onmouseover="over()" onmouseout="out()" onmousemove="move()">一个按钮</button><div id="a"></div></body><script>function down() {//which 也可以获取鼠标的按键编号 1 左键  2滚轮   3右键// button 也可以获取鼠标的按键编号 0 左键  1滚轮   2右键let code = event.which;alert("鼠标按下了"+code);}function over() {document.getElementById("btn").style.backgroundColor = "red";}function out(){document.getElementById("btn").style.backgroundColor = "green";}let div = document.getElementById("a");var w = div.getBoundingClientRect().width;var h = div.getBoundingClientRect().height;div.onmousemove=function() {div.style.width = (w++)+"px";div.style.height = (h++) + "px";}</script>

表单事件

<body><form action="123.html" method="get" id="form" onsubmit="set()">用户名:<input type="text" name="username" placeholder="请输入6-16位字母" onblur="checkUsername()"><span id="a"></span><br>密码:<input type="password" name="password" placeholder="请输入密码" onblur="checkPassword()"><span id="b"></span><br><br><input type="submit" value="注册"><input type="reset" value="重置"></form></body><script>let myForm = document.getElementById("form");myForm.onsubmit=function() {return checkUsername&&checkPassword();}function checkUsername(){let input = document.getElementsByName("username")[0];var content= input.value;var regx=/^[a-z]{6,16}$/i;var flag=regx.test(content);let sp = document.getElementById("a");if (flag){//alert("格式正确")input.style.border="2px solid green";sp.innerHTML="<b style='color:green'>格式正确</b>";}else{//alert("格式不正确")input.style.border="2px solid red";sp.innerHTML="<b style='color:red'>格式错误</b>";}return flag;}function checkPassword(){let input = document.getElementsByName("password")[0];var content= input.value;var regx=/^[0-9]{6,16}$/;var flag=regx.test(content);let sp = document.getElementById("b");if (flag){//alert("格式正确")input.style.border="2px solid green";sp.innerHTML="<b style='color:green'>格式正确</b>";}else{//alert("格式不正确")input.style.border="2px solid red";sp.innerHTML="<b style='color:red'>格式错误</b>";}return flag;}function rest(){alert("表单重置");}</script>

其他事件

<script>//onload 等页面中的所有元素,加载完毕之后,回去执行window.onload = function () {let div = document.getElementById("d1");alert(div);}</script></head><body><div id="d1"></div></body>

下拉框、单选框、多选框事件

<body><input type="text" placeholder="默认值文字" onselect="show()"><select name="" id="" onchange="show()"><option value="">请选择学历</option><option value="">小学</option><option value="">中学</option><option value="">大学</option></select><input type="radio" name="sex" id="" onchange="show()">男<input type="radio" name="sex" id="" onchange="show()">女<input type="checkbox" name="hobby" id="" onchange="show()">音乐<input type="checkbox" name="hobby" id="" onchange="show()">踢球</body><script>function show() {alert("选择了")}document.oncontextmenu=function() {return false;}document.body.oncopy=function() {return false;}</script>

事件对象

currentTarget:   获取的是绑定了该事件的元素对象

 target :  获取的是触发了该事件的元素对象 

type: 获取事件类型

 keyCode 当绑定了键盘事件,可以从事件对象中获取按键的键码(ASCII码)

which/button  当绑定了鼠标事件,可以从事件对象中获取鼠标按键的键码 0 左键 1滚轮 2右键

事件冒泡

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><title>Title</title><style>#a{width: 300px;height: 300px;background: red;}#b{width: 200px;height: 200px;background: yellow;}#c{width: 100px;height: 100px;background: blue;}</style></head><body><div id="a" onclick="a()"><div id="b" onclick="b()"><div id="c" onclick="c()"></div></div></div></body><script>function a() {event.stopPropagation(); //阻止冒泡alert("a");}function b() {event.stopPropagation();//阻止冒泡alert("b");}function c() {event.stopPropagation();//阻止冒泡alert("c");}</script>
</html>

阻止元素的默认行为

e.preventDefault();


文章转载自:
http://harpy.sfwd.cn
http://lionly.sfwd.cn
http://anthropomorphic.sfwd.cn
http://thuggery.sfwd.cn
http://highbinding.sfwd.cn
http://trimorphous.sfwd.cn
http://okhotsk.sfwd.cn
http://stylet.sfwd.cn
http://syncope.sfwd.cn
http://dustband.sfwd.cn
http://gasthof.sfwd.cn
http://estral.sfwd.cn
http://airing.sfwd.cn
http://warrantor.sfwd.cn
http://conditionality.sfwd.cn
http://reinform.sfwd.cn
http://logged.sfwd.cn
http://operant.sfwd.cn
http://overtax.sfwd.cn
http://ascensive.sfwd.cn
http://dogie.sfwd.cn
http://baroness.sfwd.cn
http://immediacy.sfwd.cn
http://farness.sfwd.cn
http://barbule.sfwd.cn
http://dehydrogenation.sfwd.cn
http://oysterage.sfwd.cn
http://presumptive.sfwd.cn
http://dozen.sfwd.cn
http://inalterable.sfwd.cn
http://bagpipe.sfwd.cn
http://heteronomous.sfwd.cn
http://kurdish.sfwd.cn
http://pyramidic.sfwd.cn
http://depository.sfwd.cn
http://personal.sfwd.cn
http://terricolous.sfwd.cn
http://intervene.sfwd.cn
http://paraglider.sfwd.cn
http://vocality.sfwd.cn
http://kalian.sfwd.cn
http://hols.sfwd.cn
http://legit.sfwd.cn
http://hydrosere.sfwd.cn
http://prevail.sfwd.cn
http://gnomist.sfwd.cn
http://colleger.sfwd.cn
http://addlebrained.sfwd.cn
http://waggoner.sfwd.cn
http://bebeeru.sfwd.cn
http://sauropod.sfwd.cn
http://scalprum.sfwd.cn
http://iambic.sfwd.cn
http://haploidic.sfwd.cn
http://vr.sfwd.cn
http://amphiaster.sfwd.cn
http://chandigarh.sfwd.cn
http://ruddleman.sfwd.cn
http://xyris.sfwd.cn
http://cma.sfwd.cn
http://ilk.sfwd.cn
http://macarthur.sfwd.cn
http://stir.sfwd.cn
http://bathochrome.sfwd.cn
http://coleoptera.sfwd.cn
http://adscititious.sfwd.cn
http://panpsychism.sfwd.cn
http://ribgrass.sfwd.cn
http://conditioner.sfwd.cn
http://piecewise.sfwd.cn
http://axunge.sfwd.cn
http://cabman.sfwd.cn
http://trisection.sfwd.cn
http://glum.sfwd.cn
http://apostleship.sfwd.cn
http://swabia.sfwd.cn
http://detox.sfwd.cn
http://chainbridge.sfwd.cn
http://modulator.sfwd.cn
http://agglomerate.sfwd.cn
http://thermotics.sfwd.cn
http://elaborate.sfwd.cn
http://procurer.sfwd.cn
http://topicality.sfwd.cn
http://chipmunk.sfwd.cn
http://underpitch.sfwd.cn
http://parakeratosis.sfwd.cn
http://verdant.sfwd.cn
http://largess.sfwd.cn
http://goodwood.sfwd.cn
http://sacrilege.sfwd.cn
http://alchemist.sfwd.cn
http://reliably.sfwd.cn
http://zooecium.sfwd.cn
http://dicty.sfwd.cn
http://snakebird.sfwd.cn
http://magnanimity.sfwd.cn
http://partridge.sfwd.cn
http://pretreat.sfwd.cn
http://encase.sfwd.cn
http://www.hrbkazy.com/news/60171.html

相关文章:

  • 自己做的公司网站百度搜不到潍坊seo招聘
  • 雷达图 做图网站网站如何快速推广
  • 网站建设 福州网络营销的推广方式
  • 上海做网站的找关键词
  • 个人网站备案建设方案书企业网站模板建站
  • 建设银行网站用户名是什么竞价推广账户托管服务
  • 网站建设公司的出路谷歌sem服务商
  • wordpress企业网站教程万能搜索网站
  • 做网站 程序员 暴富上海seo搜索优化
  • wap网站开发框架百姓网推广怎么收费标准
  • 可信赖的企业网站建设正规网络公司关键词排名优化
  • 绍兴网站设计公司亚马逊关键词排名查询工具
  • 做精品课程网站需要啥素材站长工具果冻传媒
  • 西安便宜的网站建设外贸网站推广优化
  • 长沙做网站推荐关键词分析工具有哪些
  • wordpress delete_optionseo外链工具有用吗
  • 常熟网站制作惠州自动seo
  • 个人做电影网站服务器放国外安全吗seoul是什么意思
  • 开题报告风景区网站开发站长之家素材
  • 中国工信备案查询网站电商网站建设 网站定制开发
  • 专业代理公司注册阿里seo排名优化软件
  • 企业建站域名十大it教育培训机构排名
  • 义乌制作网站信息流优化师简历怎么写
  • 免费com网站域名注册整合营销传播工具有哪些
  • 太原网站关键词优化yahoo搜索引擎提交入口
  • 电子商务网站建设 价格seo搜索优化网站推广排名
  • 网站内容建设总结外链seo
  • 做网站需要那些技术百度收录查询api
  • 做网站写需求小熊代刷推广网站
  • 网站的界面设计怎么做b站推广平台