Home  >  Article  >  Web Front-end  >  JavaScript学习笔记之JS事件对象_基础知识

JavaScript学习笔记之JS事件对象_基础知识

WBOY
WBOYOriginal
2016-05-16 16:18:321326browse

事件对象:当事件发生时,浏览器自动建立该对象,并包含该事件的类型、鼠标坐标等。

  事件对象的属性:格式:event.属性。

  一些说明:

  event代表事件的状态,例如触发event对象的元素、鼠标的位置及状态、按下的键等等;

  event对象只在事件发生的过程中才有效。

  firefox里的event跟IE里的不同,IE里的是全局变量,随时可用;firefox里的要用参数引导才能用,是运行时的临时变量。

  在IE/Opera中是window.event,在Firefox中是event;

  而事件的对象,在IE中是window.event.srcElement,在Firefox中是event.target,Opera中两者都可用。

绑定事件

  在JS中为某个对象(控件)绑定事件通常可以采取两种手段:

  首先在head中定义一个函数:

复制代码 代码如下:

   

  绑定事件的第一种方法:

      

  绑定事件的第二种方法:

复制代码 代码如下:

      
      

其他实例

  实例1:

复制代码 代码如下:




eventTest.html




<script><br /> function mOver(object) {<br /> object.color = "red";<br /> }<br /> function mOut(object) {<br /> object.color = "blue";<br /> }<br /> </script>


            onclick="window.location.href='http://www.baidu.com'"
        onmouseover="mOver(this)" onmouseout="mOut(this)">欢迎访问



  实例2:

复制代码 代码如下:




eventTest2.html






   


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn