Event对象之鼠标事件

本文详细介绍了JavaScript中的Event对象,特别是与鼠标事件相关的属性和方法,包括onclick、oncontextmenu、ondblclick等10种事件的HTML标签支持、浏览器兼容性和语法使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Event 对象

Event 对象

Event 对象代表事件的状态,比如事件在其中发生的元素、键盘按键的状态、鼠标的位置、鼠标按钮的状态。

事件通常与函数结合使用,函数不会在事件发生前被执行!

事件句柄 (Event Handlers)

HTML 4.0 的新特性之一是能够使 HTML 事件触发浏览器中的行为,比如当用户点击某个 HTML 元素时启动一段 JavaScript。下面是一个属性列表,可将之插入 HTML 标签以定义事件的行为。

图片不见了

提示
  • 写在非script标签里面的JavaScript代码,如:onclick="fn()"。会自动进行 eval 转化,里面的任何字符都会当成代码来执行,如onclick='return false'
  • 写在script标签里的代码,如:onclick=someCode
    一般情况下,为函数。
  • 下面事件属性所有语法皆为script标签的用法。

鼠标事件

1. onclick

在元素被点击时发生

HTML标签支持
除了 :<base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, 和 <title>以外,
都支持。
浏览器支持
googleIEfirefoxsafariopera
truetruetruetruetrue
语法

ElementObject.onclick=function

<!doctype html>
<html lang="en">
    <head>
        <meta charset="UTF-8"/>
        <title>zsh</title>
    </head>
    <body>
        <div id="div" name="My div" style='height:500px;'></div>
    </body>
    <script>
    div.onclick=function(){
        alert("图像加载出错,请重新刷新页面。");
    }
    </script>
</html>

2. oncontextmenu

在元素中用户右击鼠标时触发并打开上下文菜单

浏览器支持
googleIEfirefoxsafariopera
truetruetruetruetrue
注意

所有浏览器都支持 oncontextmenu 事件, contextmenu 元素只有 Firefox 浏览器支持。

语法

ElementObject.oncontextmenu=function

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <title>zsh</title>
</head>
<body>
    <input id="div" name="My div"/>
</body>
<script>
    div.oncontextmenu = function(){
        return false;
    }
</script>
</html>

3. ondblclick

在对象被双击时发生

HTML标签支持
除了 :<base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, 和 <title>以外,
都支持。
浏览器支持
googleIEfirefoxsafariopera
truetruetruetruetrue
语法

ElementObject.ondblclick=function

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <title>zsh</title>
</head>
<body>
    <input id="div" name="My div"/>
</body>
<script>
    div.ondblclick = function(){
        alert(1);
    }
</script>
</html>

4. onmousedown

在鼠标按键被按下时发生

提示:

与 onmousedown 事件相关连得事件发生次序( 鼠标左侧/中间 按钮):
* onmousedown
* onmouseup
* onclick

与 onmousedown 事件相关连得事件发生次序 (鼠标右侧按钮):
* onmousedown
* onmouseup
* oncontextmenu

HTML标签支持
除了 :<base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, 和 <title>以外,
都支持。
浏览器支持
googleIEfirefoxsafariopera
truetruetruetruetrue
语法

ElementObject.onmousedown=function

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <title>zsh</title>
</head>
<body>
    <input id="div" name="My div"/>
</body>
<script>
    div.onmousedown = function(){
        alert(1);
    }
</script>
</html>

5. onmouseenter

鼠标指针移动到元素上时触发

提示:
  • 该事件通常与 onmouseleave 事件一同使用, 在鼠标指针移出元素上时触发。
  • onmouseenter 事件类似于 onmouseover 事件。 唯一的区别是 onmouseenter 事件不支持冒泡 。
HTML标签支持
除了 :<base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, 和 <title>以外,
都支持。
浏览器支持
googleIEfirefoxsafariopera
30.05.5true6.111.5
语法

ElementObject.onmouseenter=function

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <title>zsh</title>
</head>
<body>
<div id='div'>
    <input name="My div"/>
</div>
</body>
<script>
    div.onmouseenter = function(){
        console.log(1);
    }
</script>
</html>

6. onmouseleave

鼠标移除元素时触发

提示:
  • 该事件通常与 onmouseenter 事件一起使用, 该事件在鼠标移动到元素上时触发。
  • onmouseleave 事件类似于 onmouseout 事件。 唯一的区别是 onmouseleave 事件不支持冒泡 。
HTML标签支持
除了 :<base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, 和 <title>以外,
都支持。
浏览器支持
googleIEfirefoxsafariopera
30.05.5true6.111.5
语法

ElementObject.onmouseleave=function

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <title>zsh</title>
</head>
<body>
<div id='div' style="height:12px;border:1px solid red;">
    <input name="My div"/>
</div>

<div id='DD'  style="height:12px;margin-top:50px;border:1px solid red;">
    <input name="My div"/>
</div>
</body>
<script>

    var Ele = div.childNodes[1];
    var DDEle = DD.childNodes[1];

    div.onmouseleave = function(){
        var width = Ele.clientWidth;
        Ele.style.width = width + 1 + "px";
    }

    DD.onmouseout = function(){
        var width = DDEle.clientWidth;
        DDEle.style.width = width + 1 + "px";
    }

</script>
</html>

7. onmousemove

在鼠标指针移到指定的对象时发生

HTML标签支持
除了 :<base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, 和 <title>以外,
都支持。
浏览器支持
googleIEfirefoxsafariopera
truetruetruetruetrue
语法

ElementObject.onmousemove=function

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <title>zsh</title>
</head>
<body>
<div id='div' style="height:120px;border:1px solid red;"></div>
</body>
<script>
    var lastX = "";//保存上一个X的坐标点 
    div.onmousemove = function(e){
        e = e || window.event;
        if(lastX){
            if(e.pageX>lastX){
                console.log("右移");
            }else if(e.pageX<lastX){
                console.log("左移");
            }
        }
        lastX = e.pageX;
    }
</script>
</html>

8. onmouseover

鼠标指针移动到指定的元素上时发生

提示:
  • 该事件通常与 onmouseout 事件一起使用, 该事件在鼠标移动到元素上时触发。
  • onmouseover 事件类似于 onmouseenter 事件。 唯一的区别是 onmouseover 事件支持冒泡 。
HTML标签支持
除了 :<base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, 和 <title>以外,
都支持。
浏览器支持
googleIEfirefoxsafariopera
truetruetruetruetrue
语法

ElementObject.onmouseover=function

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <title>zsh</title>
</head>
<body>
<div id='div' style="height:120px;border:1px solid red;"></div>
</body>
<script>
    div.onmouseover = function(){
        console.log("你进入了我的范围");
    }
    div.onmouseout = function(){
        console.log('你离开了我的范围');
    }
</script>
</html>

9. onmouseout

在鼠标指针移出指定的对象时发生

提示:
  • 该事件通常与 onmouseover 事件一起使用, 该事件在鼠标移动到元素上时触发。
  • onmouseout 事件类似于 onmouseleave 事件。 唯一的区别是 onmouseout 事件支持冒泡 。
HTML标签支持
除了 :<base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, 和 <title>以外,
都支持。
浏览器支持
googleIEfirefoxsafariopera
truetruetruetruetrue
语法

ElementObject.onmouseout=function

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <title>zsh</title>
</head>
<body>
<div id='div' style="height:120px;border:1px solid red;"></div>
</body>
<script>
    div.onmouseover = function(){
        console.log("你进入了我的范围");
    }
    div.onmouseout = function(){
        console.log('你离开了我的范围');
    }
</script>
</html>

10. onmouseup

在鼠标按键被松开时发生

提示:

与 onmousedown 事件相关连得事件发生次序( 鼠标左侧/中间 按钮):
* onmousedown
* onmouseup
* onclick

与 onmousedown 事件相关连得事件发生次序 (鼠标右侧按钮):
* onmousedown
* onmouseup
* oncontextmenu

HTML标签支持
除了 :<base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, 和 <title>以外,
都支持。
浏览器支持
googleIEfirefoxsafariopera
truetruetruetruetrue
语法

ElementObject.onmouseup=function

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <title>zsh</title>
</head>
<body>
<div id='div' style="height:120px;border:1px solid red;"></div>
</body>
<script>
    div.onmousedown = function(){
        console.log("你点击了鼠标");
    }
    div.onmouseup = function(){
        console.log("你松开了鼠标");
    }
</script>
</html>

文档内容出自 W3cSchool和菜鸟教程, 如需查看更详细的有关内容 请登录 http://www.w3school.com.cn/http://www.runoob.com/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值