小白教程
所有教程
关于
Search
172.69.58.173
172.69.58.173
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Event mouseleave”的源代码
本页内容
上一节:
Event_mouseenter
下一节:
Event_mousemove
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:mouseleave()}}[[Category:jquery ref events|39]] = jQuery mouseleave() 方法 = [[jquery ref events|jQuery 事件方法]] 当鼠标指针离开 <p> 元素时,设置背景色为灰色: <sample title="" desc="" lang="html" hererun="1"> $("p").mouseleave(function(){ $("p").css("background-color","gray"); }); </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("p").mouseenter(function(){ $("p").css("background-color","yellow"); }); $("p").mouseleave(function(){ $("p").css("background-color","lightgray"); }); }); </script> </head> <body> <p>鼠标移动到该段落。</p> </body> </html> </run> == 定义和用法 == 当鼠标指针离开被选元素时,会发生 mouseleave 事件。 mouseleave() 方法触发 mouseleave 事件,或添加当发生 mouseleave 事件时运行的函数。 '''注意:'''与 [[event mouseout|mouseout]] 事件不同,mouseleave 事件只有在鼠标指针离开被选元素时被触发,mouseout 事件在鼠标指针离开任意子元素时也会被触发。参见页面底部演示实例。 '''提示:'''该事件通常与 [[event mouseenter|mouseenter]] 事件一起使用。 == 语法 == 触发被选元素的 mouseleave 事件: $(selector).mouseleave() <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("p").mouseenter(function(){ $("p").css("background-color","yellow"); }); $("p").mouseleave(function(){ $("p").css("background-color","lightgray"); }); $("#btn1").click(function(){ $("p").mouseenter(); }); $("#btn2").click(function(){ $("p").mouseleave(); }); }); </script> </head> <body> <p>这是一个段落。</p> <button id="btn1">触发 mouseenter 事件</button><br> <button id="btn2">触发 mouseleave 事件</button> </body> </html> </run> 添加函数到 mouseleave 事件: $(selector).mouseleave(function) <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("p").mouseenter(function(){ $("p").css("background-color","yellow"); }); $("p").mouseleave(function(){ $("p").css("background-color","lightgray"); }); }); </script> </head> <body> <p>鼠标移动到该段落。</p> </body> </html> </run> {| class="table table-striped table-hover" ! 参数 ! 描述 |- | function | 可选。规定 mouseleave 事件触发时运行的函数。 |} == 更多实例 == [[try.php?filename=tryjquery_event_mouseleave_mouseout|mouseleave() 与 mouseout() 之间的不同]] 演示 mouseleave() 与 mouseout() 之间的不同。 [[jquery ref events|jQuery 事件方法]]
返回至“
Event mouseleave
”。
上一节:
Event_mouseenter
下一节:
Event_mousemove