<script type="text/javascript"> $(function() { var li = $('.se-btn li'); var pattern = 'images/ser'; for (var i = 0; i < 9; i++) { (function(i) { var url = li.eq(i).find('span').css('background-image'); var length = url.split('').length; //获取url的长度 var ind1 = url.search(pattern); //返回字符串中 pattern开始位置 var qian = url.slice(0, ind1 + 10); //前面的字符串 var hou = url.slice(ind1 + 10); //后面的字符串 li.eq(i).hover(function() { $(this).find('span').css({ 'background-image': qian + 'h' + hou //中间加个 'h' }); }, function() { $(this).find('span').css({ 'background-image': qian + hou }); }); })(i); } }); </script>
摘自:http://www.cnblogs.com/flappyCoder/p/4106109.html
例子:http://pan.baidu.com/s/1sl4FyPJ server.html