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

想做一个自己的网站怎么做的湖南网站建设seo

想做一个自己的网站怎么做的,湖南网站建设seo,黄页公司,克隆网站怎么做一、简介 ​ 事件:发生在HTML元素上的事情,可以是用户的行为,也可以是浏览器的行为,如 用户点击了某个HTML元素用户将鼠标移动到某个HTML元素上用户输入数据时光标离开页面加载完成 ​ 事件源:事件触发的源头&#xf…

一、简介

​ 事件:发生在HTML元素上的事情,可以是用户的行为,也可以是浏览器的行为,如

  • 用户点击了某个HTML元素
  • 用户将鼠标移动到某个HTML元素上
  • 用户输入数据时光标离开
  • 页面加载完成

事件源:事件触发的源头,即触发事件的元素,如按钮、输入框、超链接等

事件对象:当一个事件发生时,这个事件相关的详细信息会被保存到一个对象中,称为event对象

事件监听:监听事件的发生,绑定事件函数,当事件被触发后执行该事件函数,即回调函数

二、绑定事件

​ 三种方式:

  • 静态绑定,通过为标签的事件属性赋值
<head>    <script>function f1(){console.log("静态绑定");}</script>
</head>
<body><!--方式1:通过标签的事件属性--><button onclick="f1()">按钮1</button>
</body>
  • 动态绑定,通过为DOM对象的事件属性赋值
<head><script>//当页面加载完成后执行window.onload=function(){var btn2=document.getElementById("btn2");//方式2:通过DOM对象的事件属性,为按钮绑定点击事件btn2.onclick=function(){console.log("动态绑定");}}</script>
</head>
<body><button id="btn2">按钮2</button>
</body>
  • 动态绑定,通过为DOM对象进行事件监听,使用addEventListene("事件名",回调函数)
<head>   <script>//当页面加载完成后执行window.onload=function(){//方式3:通过为DOM对象进行事件监听addEventListenervar btn3=document.getElementById("btn3");btn3.addEventListener("click",function(){console.log("动态绑定");});}</script>
</head>
<body><button id="btn3">按钮3</button>
</body>

 注意:

  • 可以通过事件回调函数的第一个参数获取事件对象event,属性含义:
    target      事件的目标元素,即事件源type            事件类型timeStamp   事件生成的日期和时间clientX     当事件被触发时,鼠标指针的水平坐标clientY     当事件被触发时,鼠标指针的垂直坐标
  • 在事件回调函数中,this表示事件源,即发生事件的元素 
<head><script>//当页面加载完成后执行window.onload=function(){var btn=document.getElementById("btn");btn.onclick=function(event){//事件触发时会自动为回调函数传入一个参数,表示event事件对象console.log(111);console.log(event);console.log(typeof event);console.log(event.target);//事件源console.log(event.type);//事件类型console.log(this);//事件源,等同于console.log(event.target);}}function f1(event){console.log(event);}</script>
</head>
<body><button id="btn">点我</button><!--静态绑定事件时,需要在绑定事件回调函数时接收事件对象参数--><button onclick="f1(event)">click me</button>
</body>

 三、常用事件

1、鼠标事件

事件名描述
onclick鼠标单击
ondblclick鼠标双击
onmouseover鼠标移到某元素之上
onmouseout鼠标从某元素上移开
onmousedown鼠标按钮被按下
onmouseup鼠标按键被松开
onmousemove鼠标被移动
oncontextmenu鼠标右键单击
<head><script>window.onload=function(){var btn=document.getElementById("btn");//鼠标事件//btn.onclick=function(){}//鼠标单击//btn.ondblclick=function(){}//鼠标双击/*btn.onmouseover=function(){//鼠标经过console.log(111);}btn.onmouseout=function(){//鼠标离开console.log(222);}btn.onmousedown=function(){//鼠标按下console.log(111);}btn.onmouseup=function(){//鼠标松开console.log(222);}btn.onmousemove=function(){//鼠标移动console.log(111);}*/btn.oncontextmenu=function(){//鼠标右击console.log(111);}}</script>
</head>
<body><button id="btn">点我</button>
</body>

2、键盘事件

事件名描述
onkeydown某个键盘的键被按下去
onkeypress某个键盘的键被按下去且松开
onkeyup某个键盘的键被松开
<head>     <script>//键盘事件var username=document.getElementById("username");username.onkeydown=function(e){//键盘的键被按下去//console.log(111);//console.log(e.keyCode);//获取按键码if(e.keyCode==13){//当输入回车后才会输出111(回车键码是13)console.log(111);}}/*username.onkeypress=function(){//键盘的键被按下去且松开console.log(222);}username.onkeyup=function(){//键盘的键被松开console.log(333);}*/}</script>
</head>
<body>用户名:<input type="text" id="username"> 
</body>

3、表单事件

事件名描述
onfocus元素获得焦点
onblur元素失去焦点
onchange域的内容发生改变,一般用于文件选择器和下拉列表
onselect文本内容被选中
onsubmit表单提交前触发,回调函数返回true表示允许表单提交,返回false表示阻止表单提交

表单元素的方法:focus()、blur()、select()、click()

<head><style>#username,#email{outline: 0;}.border{border: 1px solid red;}#img{width:100px;height:100px;}#head{display:none;}/*被选中的内容为红色*//*#email::selection{color:red;}*/</style><script>//表单事件window.onload=function(){var username=document.getElementById("username");username.onfocus=function(){//元素获得焦点username.classList.add("border");}username.onblur=function(){//元素失去焦点username.classList.remove("border");}document.getElementById("eat").onchange=function(){//域的内容发生改变console.log(this.checked);//获取是否选中}document.getElementById("head").onchange=function(){//console.log(this.files);//获取选择的文件数据document.getElementById("img").src=window.URL.createObjectURL(this.files[0]);//根据选中的图片改src}document.getElementById("email").onselect=function(){//文本内容被选中this.classList.add("border");}document.getElementById("frm").onsubmit=function(){//判断表单内容是否符合要求var email=document.getElementById("email").value;if(email==""){return false;}return true;}}</script>
</head>
<body><form action="" id="frm">用户名:<input type="text" id="username"><br>爱好:<input type="checkbox" name="hobby" id="eat" value="eat">吃饭<br>头像:<input type="file"  id="head" multiple><!--可同时选多个文件--><label for="head"><img src="./默认头像.png" id="img"></label><br>邮箱:<input type="text" id="email" value="tom@sina.com.cn" name="email"><br><input type="submit" value="提交"></form>
</body>

四、事件操作

1、事件流

概念:当一个HTML元素产生事件时,该事件会在当前元素与根元素之间按特定的顺序传播,所有经过的节点都会收到该事件并执行,这个传播过程就是DOM事件流。

​ 分类:事件冒泡、事件捕获

2、事件冒泡/事件捕获

事件冒泡:当一个元素上的事件被触发时,事件从事件源开始,往上冒泡直到页面的根元素,这一过程被称为事件冒泡(默认方式)

事件捕获:当一个元素上的事件被触发时,事件从页面的根元素开始,往下直到事件目标元素,这一过程被称为事件捕获

阻止事件冒泡 :event.stopPropagation()

<head><style>div{border:1px solid black;}#div1{width:200px;height:200px;}#div2{width:150px;height:150px;}#div3{width:100px;height:100px;}#div4{width:50px;height:50px;}</style><script>function $(id){return document.getElementById(id);}window.onload=function(){$("div1").addEventListener("click",function(){console.log("div1");},false)//第三个参数为true表示采用事件捕获,默认为false表示事件冒泡$("div2").addEventListener("click",function(){console.log("div2");},false)$("div3").addEventListener("click",function(event){console.log("div3");event.stopPropagation();//阻止事件传播},false)$("div4").addEventListener("click",function(){console.log("div4");},false)};</script>
</head>
<body><div id="div1" onclick="print(' div1' )">div1<div id="div2" onclick="print(' div2' )">div2<div id="div3" onclick="print(' div3' )">div3<div id="div4" onclick="print(' div4' )">div4</div></div></div></div>
</body>

 3、事件代理/事件委托

​ 概念:利用事件冒泡/事件捕获机制,通过给父元素绑定事件,从而实现对所有子元素的事件管理,无需为每个子元素绑定事件

​ 优点:1.减少事件注册,降低内存占用

​            2.新增元素时实现动态绑定事件

<head>     <style>ul{border:1px solid #ccc;}li{background: pink;}</style><script>window.onload=function(){/*var lis=document.querySelectorAll("li");for(var li of lis){li.onclick=function(){console.log(this);//事件源console.log(this,innerText);}}*/document.querySelector('ul').onclick=function(e){console.log(e.target.innerText);//console.log(e.target);//触发事件的原始对象console.log(this);}}function add(){var li=document.createElement("li");li.innerText="li6";/*li.onclick=function(){console.log(this,innerText);}*/document.querySelector('ul').appendChild(li);}</script>
</head>
<body><button onclick="add()">添加li</button><ul><li>li1</li><li>li2</li><li>li3</li><li>li4</li><li>li5</li></ul>
</body>

4、事件默认行为

​ 概念:当一个事件发生时浏览器自己会默认做的事情,如:点击链接时默认会跳转,右键点击时默认会弹出菜单

​ 阻止事件的默认行为:e.preventDefault();

<head> <script>function print(e){console.log(111);e.preventDefault();//阻止事件的默认行为}</script>
</head>
<body><button oncontextmenu="print(event)">右键点击</button><br><a href="https://www.baidu.com" onclick="print(event)">百度</a><!--<a href="JavaScript:print()">百度</a>-->
</body>

 


文章转载自:
http://amgot.c7627.cn
http://aleppo.c7627.cn
http://chlamydeous.c7627.cn
http://namesake.c7627.cn
http://suva.c7627.cn
http://highflyer.c7627.cn
http://certification.c7627.cn
http://diversification.c7627.cn
http://lych.c7627.cn
http://unenlightened.c7627.cn
http://shily.c7627.cn
http://involute.c7627.cn
http://onlooking.c7627.cn
http://ricksha.c7627.cn
http://yarraman.c7627.cn
http://squilgee.c7627.cn
http://phoniatrics.c7627.cn
http://rurp.c7627.cn
http://bubbler.c7627.cn
http://maid.c7627.cn
http://cuetrack.c7627.cn
http://lekker.c7627.cn
http://strategics.c7627.cn
http://languorous.c7627.cn
http://indianapolis.c7627.cn
http://apartotel.c7627.cn
http://paramilitary.c7627.cn
http://usar.c7627.cn
http://endure.c7627.cn
http://convivially.c7627.cn
http://esophagean.c7627.cn
http://pennon.c7627.cn
http://furcation.c7627.cn
http://downslope.c7627.cn
http://untraceable.c7627.cn
http://hyte.c7627.cn
http://phosphorous.c7627.cn
http://fissile.c7627.cn
http://ensphere.c7627.cn
http://cankerous.c7627.cn
http://percale.c7627.cn
http://explosible.c7627.cn
http://vologda.c7627.cn
http://encopresis.c7627.cn
http://neuromata.c7627.cn
http://presumptuous.c7627.cn
http://cornopean.c7627.cn
http://novato.c7627.cn
http://chonju.c7627.cn
http://splat.c7627.cn
http://punster.c7627.cn
http://infructescence.c7627.cn
http://minority.c7627.cn
http://solatium.c7627.cn
http://intwine.c7627.cn
http://unsubmissive.c7627.cn
http://turbodrill.c7627.cn
http://fuji.c7627.cn
http://logarithm.c7627.cn
http://tenon.c7627.cn
http://corybantism.c7627.cn
http://achilles.c7627.cn
http://mulki.c7627.cn
http://upthrow.c7627.cn
http://hyfil.c7627.cn
http://sst.c7627.cn
http://inconcinnity.c7627.cn
http://uneven.c7627.cn
http://trey.c7627.cn
http://incongruously.c7627.cn
http://madness.c7627.cn
http://hirable.c7627.cn
http://narcoleptic.c7627.cn
http://charlatanism.c7627.cn
http://compendia.c7627.cn
http://supercontract.c7627.cn
http://sandpaper.c7627.cn
http://cocker.c7627.cn
http://thiophosphate.c7627.cn
http://neuropath.c7627.cn
http://valvelet.c7627.cn
http://contextualize.c7627.cn
http://bughouse.c7627.cn
http://monitory.c7627.cn
http://obesity.c7627.cn
http://amenophis.c7627.cn
http://crucifixion.c7627.cn
http://jabez.c7627.cn
http://wiredrawing.c7627.cn
http://sparkle.c7627.cn
http://cutline.c7627.cn
http://vectorcardiogram.c7627.cn
http://bfc.c7627.cn
http://superheat.c7627.cn
http://neurohormone.c7627.cn
http://suspicious.c7627.cn
http://megacephaly.c7627.cn
http://brassily.c7627.cn
http://kilomegcycle.c7627.cn
http://loiasis.c7627.cn
http://www.zhongyajixie.com/news/101542.html

相关文章:

  • 英文网站建设官网上海seo推广方法
  • laravel 做中英文网站百度经验发布平台
  • 用hexo做网站网站模板搭建
  • 精选网站建立 推广 优化成都网站seo诊断
  • 卡地亚手表官方网站辽宁seo推广
  • 分析网站日志seo如何建立优化网站
  • 抚松做网站百度竞价排名公式
  • 开州网站建设中国网络优化公司排名
  • wordpress 同步 微博做搜索引擎优化的企业
  • 东莞做网站开发的公司老司机们用的关键词有哪些
  • 顺企网网站建设平台怎么推广
  • 装修公司最怕三种人天津网络优化推广公司
  • 合作网站开发公司产品免费推广网站有哪些
  • 一个企业的网站建设sem是什么职位
  • 西安本地十家做网站建设的公司营销课程培训都有哪些
  • wordpress首页模块修改seo全站优化全案例
  • 公务员可以自己做网站吗什么是关键词推广
  • 针对人群不同,网站做细分创建网站的基本步骤
  • 北京云邦网站建设百度云盘网页登录入口
  • wordpress 去掉自豪网站seo的优化怎么做
  • 网站后台登录模板做推广
  • 网站的网页设计毕业设计seo推广主要做什么的
  • 商丘网络科技有限公司谷歌seo 外贸建站
  • 小榄做网站企业网络运营师资格证
  • 站长论坛 激活网站软文广告的案例
  • wordpress建站公司俄罗斯搜索引擎yandex推广入口
  • 网站服务器在哪里最新seo黑帽技术工具软件
  • 2015做那些网站能致富网络公司网站
  • 西部数码云服务器安徽seo人员
  • 国内公司名字可以做国外网站网推接单平台