1.添加事件语法格式
<标签 事件名称="JavaScript代码"></标签>
2.JS常用鼠标事件
| 事件名称 | 作用 |
|---|---|
| onclick | 鼠标单击 |
| ondblclick | 鼠标双击 |
| onmousedown | 按下鼠标按键 |
| onmouseup | 松开鼠标按键 |
| onmouseover | 鼠标指针移动到标签上 |
| onmouseout | 鼠标从标签上移开 |
| onmousemove | 鼠标被移动 |
| onkeydown | 按下键盘按键 |
| onkeyup | 松开键盘按键 |
| onkeypress | 敲击键盘按键 |
| onchange | 用户改变域的内容 |
| onfocus | 标签获得焦点 |
| onblur | 标签失去焦点 |
| onsubmit | 提交按扭被点击 |
| onreset | 重置按扭被点击 |
| onresize | 窗口尺寸被调整 |
| onload | 页面加载完成 |
| onunload | 用户退出页面 |
3.常用事件案例
(1) onclick事件
基本语法
<标签 onclick=”JavaScript代码”></标签>
功能
代码
<!DOCTYPE html><html><head><title>JavaScript示例</title><meta charset="utf-8" /><script type="text/javascript">function hello(){alert("hello呀!");}</script></head><body><input type="button" value="按钮" style="width:100%;height:50px" onclick="hello()"/></body></html>
代码讲解
onclick=”hello()” 鼠标点击按钮执行hello函数
(2) onmousedown事件
基本语法
<标签 onmousedown=”JavaScript代码”></标签>
功能
代码
<!DOCTYPE html>
<html>
<head>
<title>JavaScript示例</title>
<meta charset="utf-8" />
<script type="text/javascript">
function hello(){
alert("hello呀!");
}
</script>
</head>
<body>
<input type="button" value="按钮" style="width:100%;height:50px" onmousedown="hello()"/>
</body>
</html>
代码讲解
onmousedown =”hello()” 按钮被按下时执行hello函数
(3)onmouseup事件
基本语法
<标签 onmouseup=”JavaScript代码”></标签>
功能
代码
<!DOCTYPE html>
<html>
<head>
<title>JavaScript示例</title>
<meta charset="utf-8" />
<script type="text/javascript">
function hello(){
alert("hello呀!");
}
</script>
</head>
<body>
<input type="button" value="按钮" style="width:100%;height:50px" onmouseup="hello()"/>
</body>
</html>
代码讲解
onmousedown =”hello()” 按钮被按下鼠标松开后执行hello函数
(4) onmousemove事件
基本语法
<标签 onmousemove=”JavaScript代码”></标签>
功能
代码
<!DOCTYPE html>
<html>
<head>
<title>JavaScript示例</title>
<meta charset="utf-8" />
<script type="text/javascript">
function hello(){
alert("hello呀!");
}
</script>
</head>
<body>
<input type="button" value="按钮" style="width:100%;height:50px" onmousemove="hello()"/>
</body>
</html>
代码讲解
onmousemove =”hello()” 鼠标在按钮内部移动时执行hello函数
(5) onmouseover事件
基本语法
<标签 onmouseover=”JavaScript代码”></标签>
功能
代码
<!DOCTYPE html>
<html>
<head>
<title>JavaScript示例</title>
<meta charset="utf-8" />
<script type="text/javascript">
function hello(){
alert("hello呀!");
}
</script>
</head>
<body>
<input type="button" value="按钮" style="width:100%;height:50px" onmouseover="hello()"/>
</body>
</html>
代码讲解
onmouseover =”hello()” 鼠标移入按钮上方执行hello函数
(6) onmouseout事件
基本语法
<标签 onmouseout=”JavaScript代码”></标签>
功能
代码
<!DOCTYPE html>
<html>
<head>
<title>JavaScript示例</title>
<meta charset="utf-8" />
<script type="text/javascript">
function hello(){
alert("hello呀!");
}
</script>
</head>
<body>
<input type="button" value="按钮" style="width:100%;height:50px" onmouseout="hello()"/>
</body>
</html>
代码讲解
onmouseout =”hello()” 鼠标在按钮上方移出后执行hello函数
4.添加onclick事件,alert()方法
<script type="text/javascript">
function showImage(){
alert("图片被点击了");
}
</script>
<!-- 图片列表 -->
<div class="image_list">
<img src="http://www.yyfun001.com/res/htmlclassics/full/images/131.jpg" title="点击查看大图" onclick="showImage()" />
效果为:当点击该图片浏览器顶端会弹出对话框,对话框内文字内容为alert( )括号内的内容
