多个jQuery mouseover mouseout

时间:2012-08-21 09:55:21

标签: jquery ajax mouseover mouseout

我的jQuery代码是:

                num=15;                     
                        $('#share_button_'+num).mouseover(function(){       
                            $('#share_button_'+num).attr("src","img/share-hover.jpg");      
                        });                         
                        $('#share_button_'+num).mouseout(function(){        
                            $('#share_button_'+num).attr("src","img/share-button-en.jpg");      
                        });

                num=30;

                        $('#share_button_'+num).mouseover(function(){                               
                            $('#share_button_'+num).attr("src","img/share-hover.jpg");      
                        });                         
                        $('#share_button_'+num).mouseout(function(){        
                            $('#share_button_'+num).attr("src","img/share-button-en.jpg");      
                        });

我的问题是只有最后一个(可以是15或30)有效。是写在Ajax成功响应中的:

        $.ajax({
        async: true,
        url: "myurl.php",
        type: "POST",
        data: "uid="+uid,
        dataType: "html",
        success: function (data, textStatus, xhr) {
                //Written here
              }

为什么会这样?

var div=new Array();
div[0]=15;
div[1]=96;
div[2]=30;
var i=2;
$('#share_button_'+div[i]).mouseover(function(){
    $('#share_button_'+div[i]).attr("src","img/share-hover.jpg");
});
$('#share_button_'+div[i]).mouseout(function(){
    $('#share_button_'+div[i]).attr("src","img/share-button-en.jpg");
});
var j=1;
$('#share_button_'+div[j]).mouseover(function(){
    $('#share_button_'+div[j]).attr("src","img/share-hover.jpg");
});
$('#share_button_'+div[j]).mouseout(function(){
    $('#share_button_'+div[j]).attr("src","img/share-button-en.jpg");
})

0 个答案:

没有答案