javascript 通过 jQuery 更改 img src:图像不会刷新

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26802073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 06:31:16  来源:igfitidea点击:

Changing img src through jQuery: image won't refresh

javascriptjqueryhtmlcordova

提问by ApheX

I'm trying to change src dynamically through jquery in a Phonegap Build application, like this

我正在尝试通过 Phonegap Build 应用程序中的 jquery 动态更改 src,如下所示

$('#photo_profile').attr('src', fullPath).one("load", function(evt) {
                console.log("load");
            }).each(function() {
              if(this.complete) $(this).load();
            });

But it seems that the img does not refresh while the "load" log is shown each time I change the src.

但是似乎每次更改 src 时都会显示“加载”日志时 img 不会刷新。

fullPathis something like file:///storage/emulated/0/MyAppFolder/Media/Profile%20Photos/profile.jpg

fullPath就像 file:///storage/emulated/0/MyAppFolder/Media/Profile%20Photos/profile.jpg

And it's a valid path, as if I kill the app, then re-launch it, the displays the correct image.

这是一个有效的路径,就好像我杀死了应用程序,然后重新启动它,显示正确的图像。

Am I doing something wrong? Thanks

难道我做错了什么?谢谢

回答by dfsq

Sounds like caching issue. Try to prevent it with some random parameter:

听起来像缓存问题。尝试使用一些随机参数来防止它:

$('#photo_profile').prop('src', fullPath + '?' + Math.random())

Also srcis a property, so it makes sense to use propinstead of attr.

Alsosrc是一个属性,因此使用prop代替 是有意义的attr