当前位置: 首页 > 网络学院 > 客户端脚本教程 > JavaScript > JS 事件

JavaScript
JS 字符串
JS Date
JS数组,JS Array
JS Boolean
JS Math
JS HTML DOM
JS Browser
JS Cookies
JS 校验
JS Animation
JS Image Maps
JS Timing
JS 建立对象
JS 摘要
JS 实例
JS 对象实例
JS DOM 实例
JS数组对象参考
JS布尔对象参考
JS日期对象参考

JavaScript 中的 JS 事件


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 1046 ::
收藏到网摘: n/a

Events are actions that can be detected by JavaScript.
JS可以察觉到事件有动作


Events
事件

By using JavaScript, we have the ability to create dynamic web pages. Events are actions that can be detected by JavaScript.
通过使用JS我们能够建立动态的web页面.JS能够察觉到事件有动作

Every element on a web page has certain events which can trigger JavaScript functions. For example, we can use the onClick event of a button element to indicate that a function will run when a user clicks on the button. We define the events in the HTML tags.
web页中的每个元素都能够有触发JS函数的事件。举例来说,我们可以使用按钮元素的onClick事件来让函数在用户点击按钮的时候运行。

Examples of events:
事件例子:

  • A mouse click
    鼠标的点击
  • A web page or an image loading
    web页或是图象在加载的时候
  • Mousing over a hot spot on the web page
    移动到热点的时候 (这里我怀疑Mousing是Moving)
  • Selecting an input box in an HTML form
    在HTML表单中选择一个输入框
  • Submitting an HTML form
    提交一份HTML表单
  • A keystroke
    一次击键

The following table lists the events recognized by JavaScript:
下面的表格中会罗列出JS认可的事件:

Note: Events are normally used in combination with functions, and the function will not be executed before the event occurs!
注意:事件通常是用函数来组合使用的,而且在事件发生前函数是不会执行的!

For a complete reference of the events recognized by JavaScript, go to our complete Event reference.[完整的事件参考]


onload and onUnload
加载和未加载

The onload and onUnload events are triggered when the user enters or leaves the page.
当用户进入或是离开页面的时候onload 和onUnload事件就会被触发。

The onload event is often used to check the visitor's browser type and browser version, and load the proper version of the web page based on the information.
onload事件经常 用来检测访问者的浏览器类型和版本,并以此来提供合适的页面。

Both the onload and onUnload events are also often used to deal with cookies that should be set when a user enters or leaves a page. For example, you could have a popup asking for the user's name upon his first arrival to your page. The name is then stored in a cookie. Next time the visitor arrives at your page, you could have another popup saying something like: "Welcome John Doe!".
它们还经常用来处理用户进入或离开时cookies的处理。例如,当有用户第一次访问你的网站时要得到对方的姓名,该信息就能保存在cookies中。下次同一访问者来到你的页面时就可以在页面上显示“欢迎您,某某某(姓名)”


onFocus, onBlur and onChange

The onFocus, onBlur and onChange events are often used in combination with validation of form fields.
这些事件经常组合起来使用在表单的验证上。

Below is an example of how to use the onChange event. The checkEmail() function will be called whenever the user changes the content of the field:
下面的例子将说明怎样使用onChange事件,当用户改变内容的时候checkEmail()函数就被激活

<input type="text" size="30"
id="email" onchange="checkEmail()">;

 


onSubmit(提交)

The onSubmit event is used to validate ALL form fields before submitting it.
onSubmit事件被用来校验“所有“被提交的表单

Below is an example of how to use the onSubmit event. The checkForm() function will be called when the user clicks the submit button in the form. If the field values are not accepted, the submit should be cancelled. The function checkForm() returns either true or false. If it returns true the form will be submitted, otherwise the submit will be cancelled:
下面就是一个怎样使用onSubmit事件的例子。当用户点击提交按钮就会激活函数checkForm()。如果将要提交的值不能被接受,那就会取消提交。checkForm()函数会返回真或假。返回真的话就会提交表单,假就会取消提交:

<form method="post" action="xxx.htm"

onsubmit="return checkForm()">

 


onMouseOver and onMouseOut(鼠标动作)

onMouseOver and onMouseOut are often used to create "animated" buttons.
这两个事件经常用来建立”活力(动感十足)“按钮

Below is an example of an onMouseOver event. An alert box appears when an onMouseOver event is detected:
下面就是一个an onMouseOver事件的例子。当鼠标移动到连接上的时候就会出现警告框:

<a href="http://www.w3schools.com"
onmouseover="alert('An onMouseOver event');return false">
<img src="w3schools.gif" width="100" height="30">

</a>

评论 (1) 1 All

登陆 | 还没注册?