jquery div 切换背景

<script language="JavaScript">
        var index = 0;
        var images = ["${ctx}/resources/images/dj10.jpg", "${ctx}/resources/images/delete.gif"];
      /*  $(document).ready(function () {
            $("#btn_img").click(
                    function () {
                        switch_background();
                    }
            );

        });*/
        $(function(){
            $("#btn_img").click(
                    function () {
                        switch_background();
                    }
            );
        });
        function switch_background() {
            if (index == images.length - 1) {
                index = 0;
            } else {
                index++;
            }
            document.getElementById("btn_img").style.backgroundImage = "url(" + images[index] + ")";
        }
    </script>
</head>
<body>
<div id="btn_img" style="width: 50px;height: 10px; background-image: url(${ctx}/resources/images/dj10.jpg)"></div>
</body>
</html>

相关推荐