04_Javascript初步第三天
事件
- 内联模型、脚本模型,DOM2级模型
<!--内联模型-->
<input type="button" value="bt1" onclick="alert('I am 内联模型1');">
<input type="button" value="bt2" onclick="f();">
//脚本模型
<input type="button" value="bt3" id="bt3">
<script type="text/javascript">
function f(){
alert('I am 内联模型2');
}
var bt3=document.getElementById('bt3');
bt3.onclick=function(){
alert('I am 脚本模型');
alert(this.tagName);//输出:INPUT
}
</script> <input type="button" value="bt1" id="bt1">
<script type="text/javascript">
var bt1=document.getElementById('bt1');
bt1.onclick=function(e){
var e=e||window.event;//兼容性处理,IE支持window.event
alert(e);//输出:Object mouseEvent
// alert(e.type);//输出:click
} - 事件流:冒泡与捕获
<div id="box1" style="background:lightgreen;width: 100px;height: 100px;" >
<input type="button" value="btn1" id="btn1">
</div>
<script type="text/javascript">
<!--事件冒泡,依次触发-->
var btn1=document.getElementById('btn1');
var box1=document.getElementById('box1');
btn1.onclick=function(){alert('btn1 click!');}
box1.onclick=function(){alert('box1 click!');}
document.body.onclick=function(){alert('body click!');}
document.documentElement.onclick=function(){alert('html click!');}
</script> //取消冒泡
var e=e || window.event;
e.stopPropagation();
//取消冒泡,兼容性处理
var e=e || window.event;
if(typeof e.cancelBubble=='undefined'){
e.stopPropagation();
}else{
e.cancelBubble=true;
}
} <!--绑定事件,增加第三个参数为true表示使用事件捕获-->
var btn1=document.getElementById('btn1');
var box1=document.getElementById('box1');
btn1.addEventListener('click',function(){
alert('btn1 click!');
},true);
box1.addEventListener('click',function(){
alert('box1 click!');
},true);
document.body.addEventListener('click',function(){
alert('body click!');
},true);
document.documentElement.addEventListener('click',function(){
alert('html click!');
},true);
document.addEventListener('click',function(){
alert('document click!');
},true)
- btn1.onclick=……等价于 btn.addEventListener(……)
- 事件选择
var btn1=document.getElementById('btn1');
function handler(e){
switch(e.type){
case('click'):alert('clicked!');break;
case('mouseover'):e.target.style.backgroundColor='red';break;
case('mouseout'):e.target.style.backgroundColor='';break;
}
}
btn1.onclick=handler;
btn1.onmouseover=handler;
btn1.onmouseout=handler; - 事件对象的三个阶段
var btn1=document.getElementById('btn1');
btn1.onclick=function(e){
alert(e.eventPhase);
}//输出2,目标阶段
document.body.addEventListener('click',function(e){
alert(e.eventPhase);
},true);//输出1,捕获阶段
document.body.onclick=function(e){
alert(e.eventPhase);//输出3,冒泡阶段
} - onload事件
var btn1=document.getElementById('btn1');
var img1=document.getElementById('img1');
eventUtil.addHandler(img1,'load',function(e){
alert(this.src);
}); - 表单验证: 插件:validform
function check(){
var un=document.getElementById('username').value;
var pw=document.getElementById('pw').value;
var em=document.getElementById('email').value;
if(!/^[a-zA-Z0-9_]+$/.test(un)){
alert('用户名:'+un+'不合法!');
return false;
}
if(/^[\s\r\t\n]*$/.test(pw)){
alert('密码不能为空!');
return false;
}
if(!/^([\w\.\-]+)@([\w\-]+)\.([a-z]{2,4})$/.test(em)){
alert('邮箱不合法!');
return false;
}
return true;
}
04_Javascript初步第三天的更多相关文章
- verilog阻塞与非阻塞的初步理解(三)
下面这段源码是因为习惯不好,出现不正确波形的例子. module pwm_division(reset,clkin,clkout); input reset,clkin; output clkout; ...
- 04_Javascript初步第二天(下)
错误对象 try{ aa();//这是一个未被定义的方法 }catch(e){ alert(e.name+":"+e.message);//输出:ReferenceError:aa ...
- 04_Javascript初步第一天
视频来源:麦子学院[看来是麦子的忠实粉丝] 讲师:King我只是想记住对应的中文翻译:Uncaught SyntaxError: missing ) after argument list//属性列表 ...
- Rust初步(三):使用atom搭配racer进行rust编程
在rust.cc社区中有一个关于rust编辑器的讨论(话说很多人要学一个新语言,都会立即考虑编辑器的问题,包括我在内),主要关注的是,智能提示(这个真的太重要了).大家讨论下来有几个选择 1. ecl ...
- swift学习初步(三)--控制流操作
在上一篇博客里面,我谈到了swift里面的一些基本类型以及相关的操作,相信你看了之后一定会觉得其实swift也不难嘛.好吧,这篇博客里面要谈的一些高级操作,可能会让你有点头疼了. 好了,废话不多说了, ...
- Jmeter初步使用三--使用jmeter自身录制脚本
今日,小编在网上看到很多人使用badboy来录制,然后再把jmx脚本弄到Jmeter上做性能测试.这种方法在小编刚用Jmeter时也曾经用过,但是感觉太麻烦了,所以就找了下其它资料.结果,小编偶然发现 ...
- 04_Javascript初步第二天(上)
全局函数 isFinity() - 检测是否是无穷值(+infinity,-infinity,NaN返回false): isNaN() - 检测是否为数值 encodeURI(uri) - 将字符串编 ...
- redis 初步认识三(设置登录密码)
1.cmd 2.cd C:\Program Files\Redis 3.redis-cli.exe -h 127.0.0.1 -a 123456
- Jquery Easy UI初步学习(三)数据增删改
第二篇只是学了加载用datagrid加载数据,数据的增删改还没有做,今天主要是解决这个问题了. 在做增删改前需要弹出对应窗口,这就需要了解一下EasyUi的弹窗控件. 摘自:http://philoo ...
随机推荐
- this语句
this语句 this语句调用构造器 原 因: 代码功能重复,重复会导致代码维护性低. 如何使用:this([实参]); 注意事项:构造器重载的调用,this(参数)必须写在构造方法第一行,因此 ...
- SQL Server-聚焦什么时候用OPTION(COMPILE)呢?
前言 上一篇我们探讨了在静态语句中使用WHERE Column = @Param OR @Param IS NULL的问题,有对OPTION(COMPILE)的评论,那这节我们来探讨OPTION(CO ...
- App开发 对生命周期的处理
//获取到当前所在的视图 - (UIViewController *)presentingVC:(UIApplication *)application{ UIWindow * window = ap ...
- [PHP] 编译构建最新版PHP源码
获取最新PHP代码git clone https://git.php.net/repository/php-src.git构建编译环境apt-get install build-essential 编 ...
- POJ 1739:Tony's Tour
Description A square township has been divided up into n*m(n rows and m columns) square plots (1< ...
- Shortest path of the king
必须要抄袭一下这个代码 The king is left alone on the chessboard. In spite of this loneliness, he doesn't lose h ...
- 如何在vue中使用sass
使用sass,我们需要安装sass的依赖包 npm install --save-dev sass-loader //sass-loader依赖于node-sass npm install --sav ...
- word文档自动生成方法
创建word文档需要几个接口类,常用application,document,documents,selection等.但word的功能复杂,要认识到每一个类的功能是不可能的.常用的方法是在word的 ...
- HDU 5122 K.Bro Sorting(模拟——思维题详解)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5122 Problem Description Matt's friend K.Bro is an A ...
- APP测试时常用adb命令
ADB全称Android Debug Bridge, 是android sdk里的一个工具, 用这个工具可以直接操作管理android模拟器或者真实的andriod设备(手机),故在其实工作可以给我们 ...