繁体   English   中英

如何在更新后刷新特定图像?

[英]How can I refresh a specific image after an update?

我有几个带有唯一ID的div标签,在针对特定图像的点击事件后,我正在尝试刷新它。 我怎样才能做到这一点? 这是我到目前为止的代码:

HTML:

<div class="photo_gallery_container" <?php echo($div_id); ?>>
    <table class="table table-bordered table-condensed">
        <thead >
            <tr style="background-color: #f5f5f5;">
                <th colspan="2" style="text-align: right;"><span style="cursor:pointer" id="delimg_<?php echo($id); ?>" class="delimg"><span class="label label-important">Delete</span></span></th>
            </tr>               
        </thead>
        <tbody>
            <tr>
                <td>
                    <img src="<?php echo($url['url'])?>" style="height:120px;"/>
                </td>
                <td>
                    <a href="#" id="rotateimg_<?php echo($id); ?>" class="rotateimg">Rotate</a>
                </td>
            </tr>
        </tbody>
    </table>
</div>

我的旋转点击事件如下所示:

$('.rotateimg').click(function(e) {
    e.preventDefault();
    var id = $(this).attr('id').substr(10);

    $.post("/functions/photo_functions.php", { f: 'rotate', imgid: id }, function(status){

        if (status == 'true') {

            // How can I reload the specific image after being rotated

        }
    });

});            

只需将查询字符串附加到img的URL(例如?v=1 ),这会欺骗浏览器认为它是一个新图像。

这是一个例子(假设id是页面上图像的id):

$('#' + id).prop('src', function(i, v)
{
    var separator = v.indexOf('?') == -1 ? '?' : '&';

    return v + separator + 'v=' + ( new Date() ).getTime();
});

由于看起来你的页面上的img没有ID,你可以遍历DOM来找到它,就像这样:

$(this).parent().prev().find('> img')

插件怎么样?

$.fn.refresh(function() {
  return this.each(function() {
    var $this = $(this);
    var source = $this.data('orig-src');
    var timestamp = (new Date()).getTime();

    if (!source) {
      $this.data('orig-src', $this.prop('src'));
      source = $this.data('orig-src');
    }

    if (source.indexOf('?') != -1) {
      $this.prop('src', source + '&t=' + timestamp)
    } else {
      $this.prop('src', source + '?t=' + timestamp)
    }
  });
});

现在你可以在<img />标签上调用它:

$('img.foo').refresh();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM