使用 jQuery 清除 IMG
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10865689/
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
Clear an IMG with jQuery
提问by Yarin
I'm trying to remove the loaded image from a <img>
element, but clearing or removing the src doesn't do it. What to do?
我正在尝试从<img>
元素中删除加载的图像,但清除或删除 src 不会这样做。该怎么办?
HTML:
HTML:
<img src='https://www.google.com/images/srpr/logo3w.png'>
JQUERY:
查询:
$('img').attr('src', ''); // Clear the src
$('img').removeAttr('src');? // Remove the src
Image remains...
图像仍然...
fiddle: http://jsfiddle.net/6x9NZ/
采纳答案by Quentin
回答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: Setting image src attribute not working in Chrome
我在一个类似的问题上发布了相同的答案:设置图像 src 属性在 Chrome 中不起作用
回答by AlanP
You can do this. First assign the image an ID
你可以这样做。首先为图像分配一个 ID
<img id='image_id' src='https://www.google.com/images/srpr/logo3w.png'>
And then remove the source attribute.
然后删除源属性。
jQuery('#image_id').removeAttr('src')
jQuery('#image_id').show();
回答by Michael Zelensky
what about simply hiding an element:
简单地隐藏一个元素怎么样:
$('img').hide();
You cannot remove an image from a tag. When you use jQuery, bear in mind that you do not deal with tags, you manupulate with DOM elements. Therefore, your tag in terms of jquery is not a tag, it is an object, a DOM element. And this is element is not a tag, IT IS an element that represents an image in DOM. So if you want to hide an image, you must hide the element.
您不能从标签中删除图像。当您使用 jQuery 时,请记住您不处理标签,而是使用 DOM 元素进行操作。因此,您在 jquery 中的标签不是标签,而是一个对象,一个 DOM 元素。而且 this is element 不是标签,它是表示 DOM 中图像的元素。所以如果你想隐藏一个图像,你必须隐藏元素。
回答by Giovanni Anthony Bryden Jr.
Remove the node using
使用删除节点
$("img").remove()
or simply hide the image using
或者简单地使用隐藏图像
$("img").hide()
回答by Amir Surnay
the best way is to replace image with another. i mean instead of cleaning the src, set a fake value like .attr('src','notting'); it'll load no image and the cached version of old image will be gone
最好的方法是用另一个替换图像。我的意思是不是清理 src,而是设置一个假值,如 .attr('src','notting'); 它不会加载任何图像并且旧图像的缓存版本将消失
回答by Kurt Van den Branden
If you still want the element to take up the space, but being invisible, hide it with css. Give your image a class or id and hide it like:
如果您仍然希望元素占用空间,但又不可见,请使用 css 将其隐藏。给你的图像一个类或 id 并将其隐藏,如下所示:
<img id='hide' src='https://www.google.com/images/srpr/logo3w.png'>
$('#hide').css('visibility', 'hidden');
回答by soumik sen
This worked for me.Replacing the image attribute src within div 'custom_preview_image' to blank
这对我有用。将 div 'custom_preview_image' 中的图像属性 src 替换为空白
field = jQuery(this).closest('td').find('.custom_repeatable li:last').clone(true); field.find('.custom_preview_image').removeAttr('src').end();
回答by Mahdi Alkhatib
Instead of showing broken image while setting "src"
to "#"
, There is a workaround, so you can set alt
attribute to the desired text:
而不是显示破碎的形象,而设定的"src"
到"#"
,有一种方法,这样你就可以设置alt
属性所需的文本:
$('#thumbnialPreviewImage').attr('src', '#');
$('#thumbnialPreviewImage').attr('alt', 'Please Select Image');