另一个改变src图像用jquery和替换..?

时间:2013-11-13 15:03:24

标签: jquery replace

我需要在点击预览类时更改div类的src ..

<div class="big"> <img src="image_big_1.jpg"> </div>
<div class="preview"> <img src="image_preview_2.jpg"> </div>
<div class="preview"> <img src="image_preview_3.jpg"> </div>

在这种情况下预期的确切结果:

点击

<div class="preview"> <img src="image_preview_3.jpg"> </div>

<div class="big">

src改变如下:

<div class="big"> <img src="image_big_3.jpg"> </div>

有人帮我这个,猜不到目前为止:

$('.big img').attr('src', $('.preview    img').attr('src').replace(?) );

2 个答案:

答案 0 :(得分:1)

尝试

$(document).ready(function(){
   $('.preview img').click(function(){
      var preview = $(this).attr('src');
      preview = preview.replace('preview','big');
      $('.big img').attr('src',preview);
   });
});

答案 1 :(得分:0)

尝试

jQuery(function($){
    var $bimg = $('.big img');
    $('.preview').click(function(){
        var $img = $(this).find('img');
        $bimg.attr('src', $img.attr('src').replace('preview', 'big');
    })
})