javaScript事件:

版权声明:本文为博主原创文章,转载需注明来源 https://blog.csdn.net/fsxxzq521/article/details/79164131

事件是可以被javaScript侦测到的行为。网页中的每个元素都可以产生某些可以触发javascript函数和程序的事件.

(1)onclick事件;

function add(){
}
html:
<input onclick='add()'>

(2)鼠标经过事件(onmouseover)鼠标经过事件,当鼠标移到一个对象上时,该对象就触发onmouseover事件,并执行onmouseover事件调用的程序。

function add(){
}
html:
<input onmouseover='add()'>

(3)鼠标移开事件,当鼠标移开当前对象时,执行onmouseout调用的程序。

function add(){
}
html:
<input onmouseout='add()'>

(4) 光标聚焦事件(onfocus)当网页中的对象获得聚点时,执行onfocus调用的程序就会被执行。

function add(){
}
html:
<input onfocus='add()'>

(5)失焦事件(onblur):
onblur事件与onfocus是相对事件,当光标离开当前获得聚焦对象的时候,触发onblur事件,同时执行被调用的程序。

function add(){
}
html:
<input onblur='add()'>

(6)内容选中事件(onselect)
选中事件,当文本框或者文本域中的文字被选中时,触发onselect事件,同时调用的程序就会被执行。

function add(){
}
html:
<input onselect='add()'>

(7)文本框改变事件onchange()
通过改变文本框的内容来触发onchange事件,同时执行被调用的程序。

function add(){
}
html:
<input onchange='add()'>

(8)加载事件(onload)
事件会在页面加载完成后,立即发生,同时执行被调用的程序。
注意:1. 加载页面时,触发onload事件,事件写在标签内。

  2. 此节的加载页面,可理解为打开一个新页面时。

如下代码,当加载一个新页面时,弹出对话框“加载中,请稍等…”。

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title> 加载事件 </title>
<script type="text/javascript">
  function message(){
    alert("加载中,请稍等…"); }
</script>    
</head>
<body onload='message()'>
  欢迎学习JavaScript。
</body>
</html>

(9)卸载事件(onunload)
当用户退出页面时(页面关闭、页面刷新等),触发onUnload事件,同时执行被调用的程序。注意:不同浏览器对onunload事件支持不同。

猜你喜欢

转载自blog.csdn.net/fsxxzq521/article/details/79164131