JQuery控制DIV的选取实现方法
2019/6/27 21:46:08
本文主要是介绍JQuery控制DIV的选取实现方法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
我们设置4个div 当鼠标移动到某一个div上面的时候 背景颜色就会发生变化
那我们应该知道要用到mouseover() 和 mouseout() 前一个是移动到某个位置 后面是移开某个位置
还有一个hover(,) 可以替代上面两个方法
1.用mouseover()和mouseout()
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script type="text/javascript" src="jQuery/jquery-1.12.1.js"></script> <script type="text/javascript"> $(function(){ $("div[id^='div']").mouseover(function() { $(this).css("background-color","red"); //$(this).css({"background-color":"red"}); }); $("div[id^='div']").mouseout(function() { $(this).css("background-color","#0FC"); //$(this).css({"background-color":"#0FC"}); }); }); </script> <style type="text/css"> div[id^="div"] { width:300px; height:500px; background-color:#0FC; border:1px solid black; float:left; margin-left:2px; } </style> </head> <body> <div id="div1"></div> <div id="div2"></div> <div id="div3"></div> <div id="div4"></div> </body> </html>
2.hover()
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script type="text/javascript" src="jQuery/jquery-1.12.1.js"></script> <script type="text/javascript"> $(function(){ $("div[id^='div']").hover(function(){ $(this).css("background-color","blue"); }, function(){ $(this).css("background-color","#0FC"); }); }); </script> <style type="text/css"> div[id^="div"] { width:300px; height:500px; background-color:#0FC; border:1px solid black; float:left; margin-left:2px; } </style> </head> <body> <div id="div1"></div> <div id="div2"></div> <div id="div3"></div> <div id="div4"></div> </body> </html>
也可以这么写
$(function(){ var In = function(){ $(this).css("background-color","red"); } var Out = function(){ $(this).css("background-color","yellow"); } $("p[id^='p']").hover(In,Out); });
以上这篇JQuery控制DIV的选取实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持找一找教程网。
这篇关于JQuery控制DIV的选取实现方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-30React Native常用组件-点击组件
- 2024-05-30uniapp+vue3+uv-ui手机端后台OA管理模板
- 2024-05-29Python网络爬虫的时候json=就是让你少写个json.dumps()
- 2024-05-27React Native常用组件-展示组件
- 2024-05-27React Native常用组件-列表组件
- 2024-05-09vue3开发前端表单缓存自定义指令,移动端h5必备插件
- 2024-05-09React Hooks在class组件中的使用方式
- 2024-03-30[OIDC in Action] 2. 基于OIDC(OpenID Connect)的SSO(纯JS客户端)
- 2024-03-29terraform jsonencode
- 2024-03-13vuex-persist