JavaScript 中常见的鼠标事件:

(1)鼠标按下事件(mousedown):鼠标按钮被按下时触发。

示例:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Document</title><style>.box {width: 100px;height: 100px;background-color: rgba(7, 240, 112, 0.63);}</style>
</head>
<body><div class="box"></div><script>var box = document.querySelector(".box");box.addEventListener("mousedown",function(){this.style.backgroundColor = "blue";})</script>
</body>
</html>

鼠标按下,背景颜色变为蓝色:

(2)鼠标抬起事件(mouseup):鼠标按钮抬起时触发。

示例:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Document</title><style>.box {width: 100px;height: 100px;background-color: rgba(7, 240, 112, 0.63);}</style>
</head>
<body><div class="box"></div><script>var box = document.querySelector(".box");box.addEventListener("mouseup",function(){this.style.backgroundColor = "orange";})</script>
</body>
</html>

鼠标抬起,背景颜色变为橘色:

(3)鼠标单击事件(click):鼠标单击时触发。

示例:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Document</title><style>.box {width: 100px;height: 100px;background-color: rgba(7, 240, 112, 0.63);}</style>
</head>
<body><div class="box"></div><script>var box = document.querySelector(".box");box.addEventListener("click",function(){this.style.width = "150px";})</script>
</body>
</html>

单击鼠标,宽度变为150px:

(4)鼠标双击事件(dblclick):鼠标双击时触发。

示例:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Document</title><style>.box {width: 100px;height: 100px;background-color: rgba(7, 240, 112, 0.63);}</style>
</head>
<body><div class="box"></div><script>var box = document.querySelector(".box");box.addEventListener("dblclick",function(){console.log("鼠标双击了");})
</script>
</body>
</html>

双击鼠标,控制台输出:

(5)鼠标移动事件(mousemove):鼠标移动时触发。

示例:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Document</title><style>.box {width: 100px;height: 100px;background-color: rgba(7, 240, 112, 0.63);}</style>
</head>
<body><div class="box"></div><script>var box = document.querySelector(".box");box.addEventListener("mousemove",function(){console.log("鼠标正在移动");})
</script>
</body>
</html>

鼠标在元素上移动时,控制台会不断输出:

(6)鼠标悬停事件(mouseover): 鼠标移到元素上方时触发。

示例:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Document</title><style>.box {width: 100px;height: 100px;background-color: rgba(7, 240, 112, 0.63);}</style>
</head>
<body><div class="box"></div><script>var box = document.querySelector(".box");box.addEventListener("mouseover",function(){this.style.backgroundColor = "gray";
})
</script>
</body>
</html>

鼠标位于元素上方时,背景颜色变为灰色:

(7)鼠标移出事件(mouseout):鼠标从某元素移开时触发。

示例:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Document</title><style>.box {width: 100px;height: 100px;background-color: rgba(7, 240, 112, 0.63);}</style>
</head>
<body><div class="box"></div><script>var box = document.querySelector(".box");box.addEventListener("mouseout",function(){this.style.height = "150px";
})
</script>
</body>
</html>

鼠标移开时,元素的高度会变为150px:

(8)鼠标进入事件(mouseenter):鼠标指针移动到元素上时触发。

示例:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Document</title><style>.box {width: 100px;height: 100px;background-color: rgba(7, 240, 112, 0.63);}</style>
</head>
<body><div class="box"></div><script>var box = document.querySelector(".box");box.addEventListener("mouseenter",function(){console.log("鼠标进入了");
})
</script>
</body>
</html>

鼠标进入时,控制台输出:

(9)鼠标移开事件(mouseleave):鼠标指针移出元素时触发。

示例:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Document</title><style>.box {width: 100px;height: 100px;background-color: rgba(7, 240, 112, 0.63);}</style>
</head>
<body><div class="box"></div><script>var box = document.querySelector(".box");box.addEventListener("mouseleave",function(){console.log("鼠标离开了");
})
</script>
</body>
</html>

鼠标从元素上移开时,控制台输出:

JavaScript 常见鼠标事件相关推荐

  1. java鼠标js触发事件吗,JavaScript常见鼠标事件与用法分析

    摘要:这篇JavaScript栏目下的"JavaScript常见鼠标事件与用法分析",介绍的技术点是"JavaScript.鼠标事件.鼠标.事件.用法.分析", ...

  2. JavaScript DOM / BOM (查询获取元素对象【增、删、改、查】 )以及常见鼠标事件

    1.查询获取元素(查) 1.1.根据ID获取 document.getElementById('id'); 1.2.根据标签名获取1(使用 getElementsByTagName() 方法可以返回带 ...

  3. JavaScript --- 取得鼠标事件的坐标

    说明: clientX和clientY属性:事件发生时,鼠标指针在视口中的水平和垂直坐标. pageX和pageY属性:鼠标光标在页面中的位置. screenX和screenY属性:鼠标事件发生时,鼠 ...

  4. Javascript中鼠标事件

    鼠标中的几个事件 简单的介绍一下鼠标事件的用法,如当一个注册界面要求录入信息后移开鼠标,后面显示判断录入的信息格式是否正确 <!DOCTYPE html> <html>< ...

  5. JavaScript的鼠标事件

    鼠标事件 Click 当鼠标单击时,触发 Dblclick 当鼠标双击时,触发 Mouseenter 当鼠标指针,进入元素时,触发 Mouseleave 当鼠标指针,离开元素时,触发

  6. [js点滴]JavaScript之鼠标事件04

    鼠标事件 事件种类 鼠标事件指与鼠标相关的事件,主要有以下一些. (1)click事件 click事件当用户在Element节点.document节点.window对象上,单击鼠标(或者按下回车键)时 ...

  7. js的鼠标事件(JavaScript的鼠标事件,vue的鼠标事件)

    js鼠标事件,相关属性: var div = document.getElementById("box")// 1.单击事件 onclickdiv.onclick = functi ...

  8. 面试题58:javascript中鼠标事件有哪些

    鼠标事件有: 1.click(单击)事件: 2.dblclick(双击)事件: 3.mousedown事件: 4.mouseup事件: 5.mouseout事件: 6.mouseover事件: 7.m ...

  9. javascript 鼠标事件总结

    本文转自:http://www.jb51.net/article/21590.htm javascript的鼠标事件是个比较庞大的家族.需要的朋友可以参考下. 常见的有以下8个: mousedown: ...

最新文章

  1. 青源 LIVE 第21期|中国人民大学张静:知识图谱的神经符号推理
  2. NSThread的使用
  3. linux 下 安装 phpstorm
  4. USACO3.22Stringsobits
  5. google authenticator python_Google Authenticator TOTP原理详解(以Python为例)
  6. html如何转换成电子表,如何轻松将电子表格转换为HTML [快速提示] | MOS86
  7. JavaScript之JSON详解
  8. 抽奖系统概率设计_《微博抽奖玄学理论·养号攻略XI》
  9. C语言关键字浅析-case
  10. Windows右键菜单设置与应用技巧
  11. 蚂蚁的开放:想办法摸到10米的篮筐 1
  12. 服务器 iis ftp配置文件,如何:在 IIS 中创建和配置 FTP 站点
  13. 2-13 monthCalendar日历控件
  14. 根证书、服务器证书、用户证书的区别
  15. OpenSSH 用户枚举漏洞(CVE-2018-15919)服务器修复方法(亲测实用)
  16. PYNQ系列学习(三)|pynq与zynq对比(二)
  17. [PHP] 算法-请找出带环链表的环的入口结点的PHP实现
  18. 最大熵模型(MaxEnt)解析
  19. 如何实现扫描二维码自动跳转到网页
  20. 我要学编程,看什么书好?--^_^,这里推荐一些个人觉得很不错的书

热门文章

  1. Java单元测试实践-09.Mockito的Stub参数条件
  2. 无代码白话版通俗的理解机器学习如何对未知的数据进行预测
  3. 从业多年数据分析师的亲身经验!
  4. 简单易懂的芯片科普漫画,帮你打开高深的新技术大门
  5. 上半年要写的博客文章29
  6. Android实现简单账号密码登录
  7. 为什么同门硕士进了BAT拿高工资,而博士却要挤破头进985高校?
  8. SDR软件移步BG1ICA.vicp.net:8888
  9. 百趣代谢组学分享:HSFB2b通过促进类黄酮生物合成赋予大豆耐盐能力
  10. lazada数据分析采集软件,各国市场第一视角帮你分析!