日期:2014-05-16  浏览次数:20383 次

jquery问题,请高手指点,是否应用回调还是哪里有错啊
$(".sub1 .pic ul li img").each(function(n){//li 下有6个img 选中第一个img 弹出 选好友层 选中一个好友,自动将好友头像替换掉默认img src 
$(this).click(function(){
var imgurl;
$(".sub1_1").show();//好友选择层
$(".sub1_1 .agree").click(function(){//点击确定选中一个好友
var friend_inp = $(".friends input[type='radio']:checked")[0].value; 
userinfo = friend_inp.split('|*|');
imgurl=userinfo[2];
$(".sub1_1").hide();
$(".sub1 .pic ul li img")[n].src=imgurl;
})
$(this).attr('src',imgurl); //更改图片
alert(n);
})
})


------解决方案--------------------
如果下面的效果不是你需要的,就参考做你自己的业务逻辑修改吧
HTML code
<script type="text/javascript">
$(function(){
    $(".sub1 .pic ul li img").click(function(e){
        var imgurl,index=$(this).parent().index(),_that=$(this);
        $(".sub1_1").show();//好友选择层
        $(".sub1 .pic ul li img").removeAttr("me");
        $(this).attr("me","1");
    });
    $(".sub1_1 .agree").click(function(e){//点击确定选中一个好友
        var friend_inp = $(".friends input[type='radio']:checked").val(),  
        userinfo = friend_inp.split('|*|'),
        imgurl=userinfo[2];
        $(".sub1_1").hide();
        $(".sub1 .pic ul li img[me='1']").attr("src",imgurl);
    });
})
</script>
<div class="sub1 w960">
  <div class="xz1 xz" style="display:none;"><img src="http://new-img1.ol-img.com/feature/173/2371173/img-subject/sub/d.jpg" width="22" height="22" alt=""/></div>
  <div class="xz2 xz" style="display:none;"><img src="http://new-img1.ol-img.com/feature/173/2371173/img-subject/sub/d.jpg" width="22" height="22" alt=""/></div>
  <div class="xz3 xz" style="display:none;"><img src="http://new-img1.ol-img.com/feature/173/2371173/img-subject/sub/d.jpg" width="22" height="22" alt=""/></div>
  <div class="xz4 xz"style="display:none;"><img src="http://new-img1.ol-img.com/feature/173/2371173/img-subject/sub/d.jpg" width="22" height="22" alt=""/></div>
  <div class="xz5 xz"style="display:none;"><img src="http://new-img1.ol-img.com/feature/173/2371173/img-subject/sub/d.jpg" width="22" height="22" alt=""/></div>
  <div class="xz6 xz"style="display:none;"><img src="http://new-img1.ol-img.com/feature/173/2371173/img-subject/sub/d.jpg" width="22" height="22" alt=""/></div>
  <div class="pic">
      <ul>
          <li><img src="http://new-img1.ol-img.com/feature/173/2371173/img-subject/sub/11.jpg" width="180" height="180" alt=""></li>
          <li><img src="http://new-img1.ol-img.com/feature/173/2371173/img-subject/sub/11.jpg" width="180" height="180" alt=""></li>
          <li><img src="http://new-img1.ol-img.com/feature/173/2371173/img-subject/sub/11.jpg" width="180" height="180" alt=""></li>
          <li><img src="http://new-img1.ol-img.com/feature/173/2371173/img-subject/sub/11.jpg" width="180" height="180" alt=""></li>
          <li><img src="http://new-img1.ol-img.com/feature/173/2371173/img-subject/sub/11.jpg" width="180" hei