jQuery替换scr

时间:2015-03-31 15:03:55

标签: jquery replace

在我的网站中,我使用bootstrap3和bootsnipp进行灯箱http://bootsnipp.com/snippets/featured/bootstrap-lightbox

我的代码:

        <td>
            <a href="#" class="thumbnail" data-toggle="modal" data-target="#lightbox">
                <div id="full-size-image" >
                    <img src="{{ $result->get_cover('thumbs')}}" style="height: 50px;" alt="{{ $result->type->name }}">
                </div>
            </a>
        </td>

<div id="lightbox" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
<div class="modal-dialog">
    <button type="button" class="close hidden" data-dismiss="modal" aria-hidden="true">×</button>
    <div class="modal-content">
        <div class="modal-body">
            <img src="" alt="" />
        </div>
    </div>
</div>

图片链接显示为: mysite.com/spare/thumbs/zzxx.jpg

我想仅在灯箱窗口中 / originals / 替换 / thumbs / 。 我用:

$(document).ready(function() {
var $lightbox = $('#lightbox');

$('[data-target="#lightbox"]').on('click', function(event) {

    $('#full-size-image img').attr('src', function(i, src) {
        return src.replace('thumbs','originals');
    });

    var $img = $(this).find('img'),
        src = $img.attr('src'),
        alt = $img.attr('alt'),
        css = {
            'maxWidth': $(window).width() - 100,
            'maxHeight': $(window).height() - 100
        };

    $lightbox.find('.close').addClass('hidden');
    $lightbox.find('img').attr('src', src);
    $lightbox.find('img').attr('alt', alt);
    $lightbox.find('img').css(css);});

$lightbox.on('shown.bs.modal', function (e) {
    var $img = $lightbox.find('img');
    $lightbox.find('.modal-dialog').css({'width': $img.width()});
    $lightbox.find('.close').removeClass('hidden');
});

});

但是在我的方法中,像这样,链接在整个页面中被替换。

 $('#full-size-image img').attr('src', function(i, src) {
        return src.replace('thumbs','originals');
    });

4 个答案:

答案 0 :(得分:0)

您是否尝试过这样的定义:

var src = $('#full-size-image img').attr('src');
var new_src = src.replace('thumbs','originals');
$('#full-size-image img').attr('src',new_src);

答案 1 :(得分:0)

谢谢,Wahyu Kodar。

我的方法:

$(document).ready(function() {
var $lightbox = $('#lightbox');

$('[data-target="#lightbox"]').on('click', function(event) {

    var src = $('#small-size-image img').attr('src');
    var new_src = src.replace('thumbs','originals');
    $('#full-size-image img').attr('src',new_src);

    var $img = $(this).find('img'),
        alt = $img.attr('alt'),
        css = {
            'maxWidth': $(window).width() - 100,
            'maxHeight': $(window).height() - 100
        };

    $lightbox.find('.close').addClass('hidden');
    $lightbox.find('img').attr('src', new_src);
    $lightbox.find('img').attr('alt', alt);
    $lightbox.find('img').css(css);

    });

$lightbox.on('shown.bs.modal', function (e) {
    var $img = $lightbox.find('img');
    $lightbox.find('.modal-dialog').css({'width': $img.width()});
    $lightbox.find('.close').removeClass('hidden');
});
});

<div id="lightbox" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
<div class="modal-dialog">
<button type="button" class="close hidden" data-dismiss="modal" aria-hidden="true">×</button>
<div class="modal-content">
    <div id="full-size-image" class="modal-body">
        <img src="" alt="" />
    </div>
</div>

从#small-size-image获取src并将on.click粘贴到#full-size-image new_src

答案 2 :(得分:0)

你也可以这样做:

$('#full-size-image img').prop('src', function (i, src) {
  return src.replace('thumbs', 'original');
});

答案 3 :(得分:0)

我删除了灯箱的bootsnip并给了http://ashleydw.github.io/lightbox/

a href= as original img
src = as thumb image