English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
We set4div. When the mouse moves over a certain div, the background color will change
That is, we should know that we need to use mouseover() and mouseout() before, the former is to move to a certain position, and the latter is to move away from a certain position
There is also a hover(,) that can replace the above two methods
1.use mouseover() and 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>
It can also be written like this
$(function(){ var In = function(){ $(this).css("background-color","red"); } var Out = function(){ $(this).css("background-color","yellow"); } $("p[id^='p']").hover(In,Out); });
This article on the implementation method of selecting DIV with JQuery is all the content that the editor shares with everyone. I hope it can provide a reference for everyone, and I also hope everyone will support and cheer for the tutorial.