JS 事件

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" οnchange="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"

οnsubmit="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"
οnmοuseοver="alert('An onMouseOver event');return false">
<img src="w3schools.gif" width="100" height="30">

</a>
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值