JavaScript学习与实践(12)
JS中的for...in,这个主要用于循环遍历数组或者是一个对象的所有属性,
例子:
用这个例子来遍历数组的原素,
<html>
<body>
<script type="text/javascript">
var x
var mycars = new Array()
mycars[0] = "Saab"
mycars[1] = "Volvo"
mycars[2] = "BMW"
for (x in mycars)
{
document.write(mycars[x] + "<br />")
}
</script>
</body>
</html>
JS中的事件,他是一种能够被浏览器所监听动作,
用JS我们可以创建可以交互的动态页面,事件就是这其中的主要活跃因素,每个元素都含有一定的事件属性,他们可以被JS触发
事件的例子
鼠标的移动,网页或者图片的加载,鼠标移动到网页上的 热点,选择一个下拉框的事件,提交一个表单,还有就是击键
函数和事件是紧密结合使用的,函数的发生在事件之后,
在后面给出事件的一个参考表
onload and onUnload
onload和onUnload事件是用户进入和离开页面的时候触发的
onload事件用于检查用户的浏览器类型与版本,根据这些信息来加载适当的页面版本
这两个事件主要是在用户进入页面的时候设置COOKIES,例如,用户第一次进入你的页面,你应该提示欢迎他的名字,下次进入你就应该写上直接欢迎他了like: "Welcome John Doe!".
onFocus, onBlur and onChange
onFocus, onBlur 和 onChange通常在确认表单字段的时候来一起用
下面一个例子
<input type="text" size="30" id="email" onchange="checkEmail()">
onSubmit
这个是用在提交服务器之前来验证整个表单验证的事件
<form method="post" action="xxx.htm" onsubmit="return checkForm()">
onMouseOver and onMouseOut事件
onMouseOver 和 onMouseOut通常用在创建一个灵活的按钮
例子:
<a href="http://www.w3schools.com"
onmouseover="alert('An onMouseOver event');return false">
<img src="w3schools.gif" width="100" height="30">
</a>
FF: Firefox, N: Netscape, IE: Internet Explorer
| Attribute | The event occurs when... | FF | N | IE |
|---|---|---|---|---|
| onabort | Loading of an image is interrupted | 1 | 3 | 4 |
| onblur | An element loses focus | 1 | 2 | 3 |
| onchange | The content of a field changes | 1 | 2 | 3 |
| onclick | Mouse clicks an object | 1 | 2 | 3 |
| ondblclick | Mouse double-clicks an object | 1 | 4 | 4 |
| onerror | An error occurs when loading a document or an image | 1 | 3 | 4 |
| onfocus | An element gets focus | 1 | 2 | 3 |
| onkeydown | A keyboard key is pressed | 1 | 4 | 3 |
| onkeypress | A keyboard key is pressed or held down | 1 | 4 | 3 |
| onkeyup | A keyboard key is released | 1 | 4 | 3 |
| onload | A page or an image is finished loading | 1 | 2 | 3 |
| onmousedown | A mouse button is pressed | 1 | 4 | 4 |
| onmousemove | The mouse is moved | 1 | 6 | 3 |
| onmouseout | The mouse is moved off an element | 1 | 4 | 4 |
| onmouseover | The mouse is moved over an element | 1 | 2 | 3 |
| onmouseup | A mouse button is released | 1 | 4 | 4 |
| onreset | The reset button is clicked | 1 | 3 | 4 |
| onresize | A window or frame is resized | 1 | 4 | 4 |
| onselect | Text is selected | 1 | 2 | 3 |
| onsubmit | The submit button is clicked | 1 | 2 | 3 |
| onunload | The user exits the page | 1 | 2 | 3 |
浙公网安备 33010602011771号