<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script type="text/javascript" src="js/jquery.min.js"></script> <script> (function($) { $.fn.extend({ "myhover": function(color) { $(this).hover( function() { $(this).css("background-color", color); }, function() { $(this).css("background-color", "white"); }); return $(this); } }); })(jQuery); $(function() { $("li").each(function(index) { $(this).myhover("red"); }); $("span").myhover("red"); }); </script> </head> <body> <div> <ul> <li> 1 </li> <li> 2 </li> <li> 3 </li> </ul> </div> <span>hello</span> </body> </html>
如下图: