JavaScript 事件对象的实现_javascript技巧
程序员文章站
2022-04-07 19:21:31
...
比如,我们定义了一个Classroom对象,这里我们定一个事件,当教室里的人增加超60人时就触发一个事件onFull;具体定义如下:
var Classroom=function()
{
this.numberOfPeople=0;
this.onFull=null;
this.peopleEnter=function(number)
{
this.numberOfPeople+=number;
if(this.numberOfPeople>60&&this.onFull!=null)
{
this.onFull(this.numberOfPeople);
}
}
}
function show1(number)
{
alert("教室里有"+number+"人");
}
function show2(number)
{
alert("教室里超出了"+(number-60)+"人");
}
var classroom1=new Classroom();
classroom1.onFull=show1;
classroom1.peopleEnter(30);
classroom1.peopleEnter(32);
classroom1.onFull=show2;
classroom1.peopleEnter(34);
复制代码 代码如下:
var Classroom=function()
{
this.numberOfPeople=0;
this.onFull=null;
this.peopleEnter=function(number)
{
this.numberOfPeople+=number;
if(this.numberOfPeople>60&&this.onFull!=null)
{
this.onFull(this.numberOfPeople);
}
}
}
function show1(number)
{
alert("教室里有"+number+"人");
}
function show2(number)
{
alert("教室里超出了"+(number-60)+"人");
}
var classroom1=new Classroom();
classroom1.onFull=show1;
classroom1.peopleEnter(30);
classroom1.peopleEnter(32);
classroom1.onFull=show2;
classroom1.peopleEnter(34);
推荐阅读
-
js获取元素相对窗口位置的实现代码_javascript技巧
-
javascript下给元素添加事件的方法与代码_表单特效
-
javascript模拟评分控件实现方法_javascript技巧
-
js 判断checkbox是否选中的实现代码_javascript技巧
-
引用 js在IE与FF之间的区别详细解析_javascript技巧
-
一个级联菜单代码学习及removeClass与addClass的应用_javascript技巧
-
js实现同一页面多个运动效果的方法_javascript技巧
-
javascript - AJAX是怎么样实现异步的呢?
-
来自国外的页面JavaScript文件优化_javascript技巧
-
js截取小数点后几位的写法_javascript技巧