当前位置: 首页 > 网络学院 > 客户端脚本教程 > JavaScript > JS 事件
Events are actions that can be detected by JavaScript.
JS可以察觉到事件有动作
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:
事件例子:
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.[完整的事件参考]
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中。下次同一访问者来到你的页面时就可以在页面上显示“欢迎您,某某某(姓名)”
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" |
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" |
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" |