删除图像的src属性 [英] Removing image src attribute

查看:103
本文介绍了删除图像的src属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

删除图片标签中的 src 属性似乎不会重新绘制iOS(7)上的视图,或Android(KitKat)。也不会将属性更改为空白值。桌面浏览器按预期工作。

这样可以删除属性,但视图并不反映变化:
$('#imgPreview')。removeAttr 'src');



这会改变属性,但视图并不反映更改:
$('#imgPreview')。attr('src','');



如果该属性更改为有效图像路径,视图更新:
$('#imgPreview').attr('src','http://some/image/path.jpg'); $ b

我有一个简单的测试用例这里显示这个问题。



有没有一种方法可以在更改 src 属性设置为空值或者一并删除 src 属性?

解决方案

你可以使用这种类型的代码强制元素重绘:

  $。fn.redraw = function(){
返回this.hide()。show(0);
};

或者更好:

  $。fn.redraw = function(){
return this.each(function(){
var zIndex = $(this).css('z-index') ;
$(this).css('z-index', - 1).css('z-index',zIndex);
});
};

然后将其用作例如:

  $( '#imgPreview')removeAttr( 'SRC')重绘()。; 


Removing the src attribute on an image tag doesn't seem to redraw the view on iOS(7) or Android (KitKat). Neither does changing the attribute to a blank value. Desktop browsers work as expected.

This DOES remove the attribute but the view doesn't reflect the change: $('#imgPreview').removeAttr('src');

This DOES change the attribute but the view doesn't reflect the change: $('#imgPreview').attr('src','');

If the attribute is changed to a valid image path, the view DOES update: $('#imgPreview').attr('src','http://some/image/path.jpg');

I have a simple test case HERE that shows the issue.

Is there a way that I can force a redraw after changing the src attribute to a blank value or removing the src attribute altogether?

解决方案

You could force element redraw using this kind of snippet:

$.fn.redraw = function(){
    return this.hide().show(0);
};

Or maybe better:

$.fn.redraw = function(){
    return this.each(function(){
        var zIndex = $(this).css('z-index');
        $(this).css('z-index',-1).css('z-index',zIndex);
    });
};

Then use it as e.g:

$('#imgPreview').removeAttr('src').redraw();

这篇关于删除图像的src属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆