---
title: "jquery div 切换背景"
author: "xiaowei2002"
date: "2015-05-07T11:35:03Z"
tags: []
categories: ["Web前端"]
slug: "jquery-div"
draft: false
lastmod: "2015-05-07T11:35:03Z"
id: 2209083
url: "https://xiaowei2002.iteye.com/blog/2209083"
---

&lt;script language="JavaScript"&gt;  
        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] + ")";  
        }  
    &lt;/script&gt;  
&lt;/head&gt;  
&lt;body&gt;  
&lt;div id="btn\_img" style="width: 50px;height: 10px; background-image: url(${ctx}/resources/images/dj10.jpg)"&gt;&lt;/div&gt;  
&lt;/body&gt;  
&lt;/html&gt;