jQuery 设置图像 src 属性在 Chrome 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8425817/
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
Setting image src attribute not working in Chrome
提问by user883749
I want to get the src
of one image and set it as another's src
when #btn
is pressed:
我想获取src
一个图像的 ,并将其设置为另一个图像src
时#btn
按下:
jQuery("#btn").live("click", function() {
jQuery("#another_div")
.children("img")
.attr("src", jQuery(this).prev("img").attr("src"));
jQuery(this)
.prev("img")
.attr("src","");
})
I have also tried:
我也试过:
jQuery(this).prev('img').removeAttr("src");
It works fine in Firefox, IE7-9, and Safari.
它在 Firefox、IE7-9 和 Safari 中运行良好。
Only in Chrome, the image is not removed even when the source changes (src=""
).
仅在 Chrome 中,即使源更改 ( src=""
)也不会删除图像。
回答by Joseph Marikle
You could change it to
你可以把它改成
jQuery(this).attr("src", "data:image/gif;base64,R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==");
maybe?
也许?
回答by Er. Dj
Since in my case by doing this
因为在我的情况下这样做
$(this).attr("src","");
clearing the src attribute, but, image does not vanish. Hence I just set, img tag style attribute display to none, like this
清除 src 属性,但图像不会消失。因此我只是设置,img标签样式属性显示为none,就像这样
$(this).attr("src","").css("display", "none");
It will hide the image. I hope, it will temporarily work for you.
它将隐藏图像。我希望,它会暂时为你工作。
回答by Chamika Sandamal
try remove image and append it again
尝试删除图像并再次附加
jQuery('#another_div').children("img").remove()
jQuery('#another_div').append('<img>').attr("src",jQuery(this).prev('img').attr("src"));
回答by Terry
jQuery('#btn').live("click", function() {
jQuery('#another_div')
.children("img")
.attr("src", jQuery(this).prev('img').attr("src"));
jQuery(this)
.prev('img').remove();
})
回答by Ari Okkonen
The following works well with Chrome and does not seem to give any messages to console:
以下适用于 Chrome,似乎没有向控制台提供任何消息:
image.src = "about:blank"; // Clear source path
回答by Hollowpoint
This works aswell:
这也有效:
$('img').attr('src', 'javascript:void(0)');
回答by Vlad Sabev
This worked for me:
这对我有用:
var $image = $('.my-image-selector');
$image.removeAttr('src').replaceWith($image.clone());
However, be wary of any event handlers that you might have attached to the image. There's always the withDataAndEventsand deepWithDataAndEventsoptions for jQuery clone, but I haven't tested the code with them: http://api.jquery.com/clone/#clone-withDataAndEvents
但是,请注意您可能已附加到图像的任何事件处理程序。jQuery clone总是有withDataAndEvents和deepWithDataAndEvents选项,但我还没有用它们测试代码:http: //api.jquery.com/clone/#clone-withDataAndEvents
Also, if you want to do this for multiple images, you will probably have to use something like jQuery each.
此外,如果您想对多个图像执行此操作,您可能必须使用jQuery each 之类的东西。
I posted an identical answer on a similar question: Clear an IMG with jQuery
我在一个类似的问题上发布了相同的答案:Clear an IMG with jQuery
回答by Netorica
try to set the src first to blank
尝试先将 src 设置为空白
$(this).attr("src","");
then
然后
$(this).removeAttr("src");