自定义一个jquery插件,改变鼠标滑过时的颜色

<!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>

如下图:


猜你喜欢

转载自blog.csdn.net/lplife/article/details/80423441
今日推荐