Yar*_*rin 33 html jquery image
我正在尝试从<img>
元素中删除加载的图像,但清除或删除src不会这样做.该怎么办?
HTML:
<img src='https://www.google.com/images/srpr/logo3w.png'>
Run Code Online (Sandbox Code Playgroud)
JQUERY:
$('img').attr('src', ''); // Clear the src
$('img').removeAttr('src');? // Remove the src
Run Code Online (Sandbox Code Playgroud)
图像仍然......
Vla*_*bev 10
这对我有用:
var $image = $('.my-image-selector');
$image.removeAttr('src').replaceWith($image.clone());
Run Code Online (Sandbox Code Playgroud)
但是,请注意您可能附加到图像的任何事件处理程序.jQuery clone总是有forDataAndEvents和deepWithDataAndEvents选项,但我还没有用它们测试代码:http://api.jquery.com/clone/#clone-withDataAndEvents
此外,如果您想为多个图像执行此操作,您可能必须使用类似jQuery的内容.
我在类似问题上发布了相同的答案:设置图像src属性在Chrome中不起作用
小智 6
你可以这样做.首先为图像分配ID
<img id='image_id' src='https://www.google.com/images/srpr/logo3w.png'>
Run Code Online (Sandbox Code Playgroud)
然后删除source属性.
jQuery('#image_id').removeAttr('src')
jQuery('#image_id').show();
Run Code Online (Sandbox Code Playgroud)
怎么样只是隐藏一个元素:
$('img').hide();
Run Code Online (Sandbox Code Playgroud)
您无法从标记中删除图像.当你使用jQuery时,请记住你不处理标签,你使用DOM元素进行处理.因此,您的jquery标签不是标签,它是一个对象,一个DOM元素.这是元素不是标记,IT是表示DOM中图像的元素.因此,如果要隐藏图像,则必须隐藏该元素.