日期:2014-05-17  浏览次数:20609 次

很简单的问题,帮忙解决一下,谢谢。
我想用CSS来设置一个button的背景:
即:正常不点击的时候背景是图片1.png,点击后(即:选中状态时)背景图片是2.png。
有没有好的解决方法,谢谢。
网上查到一个是这样解决的:
<script> 
var   Arr=new   Array( "1.jpg ", "2.jpg ") 
var   i=0 
function   fun(){ 
aa.style.backgroundImage= "url( "+Arr[i]+ ") " 
i=i+1 
if(i> =2)i=0 

</script> 
<table   width=500   height=100   id= "aa "   background= " "> 
<tr> <td> 
aaaa 
</td> </tr> 
</table> 
<INPUT   TYPE= "button "   value= "button "   onclick= "fun() ">
------解决方案--------------------
用div 不要用button

------解决方案--------------------
演示效果:http://job.dingso.com/crm/mail.php 我写的一个按钮的样式

<style>
.btn{  background:url(images/td_bg.jpg) 0 0 no-repeat; line-height:33px; }
.bton{ background:url(images/td_bg.jpg) 0 -34px no-repeat; line-height:33px;}
.btdown{ background:url(images/td_bg.jpg) 0 -67px no-repeat; line-height:33px;}
#button{ overflow:hidden;width:98px; height:33px; border:none;cursor:pointer;}
</style>

<input type="submit"  name="button" id="button" value="  " class="btn" />

<script type="text/javascript">
$(document).ready(function(){
$('#button').mouseover(function(){
$(this).removeClass('btn');
$(this).removeClass('btdown');
$(this).addClass('bton');
});
$('#button').mouseout(function(){
$(this).removeClass('bton');
$(this).addClass('btn');
});
$('#button').mousedown(function(){
$(this).addClass('btdown');
});
$('#button').mouseup(function(){
$(this).removeClass('btdown');
$(this).addClass('btn');
});
</script>
------解决方案--------------------
<input name="" value="" type="text" class="bg1" onmouseover="this.className='bg2'" onmouseout="this.className='bg1'"  />

两个 class   bg1 bg2 该怎么定义 不用我教了吧
------解决方案--------------------
首先指出几个错误,楼主的代码里面有很多不必要的空格,例如id= "aa "、i> =2等,这个是导致程序无法运行的一个原因,另外,aa.style.backgroundImage,其中aa没有定义,其他没什么大问题,我把详细的代码贴在下面了,在相同文件夹下放两张图片就可以运行了

<!DOCTYPE html>
<html>
<head>
<script>  
var Arr=new Array( "1.jpg", "2.jpg")  
var i=0 ;

function fun()
{
   var aa=document.getElementById("aa");
   aa.style.background="url("+Arr[i]+ ")";
   i=i+1; 
   if(i>=2)
     i=0;
}
</script>  
</head>
<body>
<table width=500 height=100 id= "aa" background="1.jpg" >  
<tr> <td>  
aaaa  
</td> </tr>  
</table>  
<input type="submit" value="Button" id="bb" onClick="fun();"/>
</body>
</html>