Clear an IMG with jQuery

后端 未结 9 2026
广开言路
广开言路 2021-02-05 01:35

I\'m trying to remove the loaded image from a element, but clearing or removing the src doesn\'t do it. What to do?

HTML:

9条回答
  •  渐次进展
    2021-02-05 02:17

    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 withDataAndEvents and deepWithDataAndEvents options for jQuery clone, but I haven't tested the code with them: 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.

    I posted an identical answer on a similar question: Setting image src attribute not working in Chrome

提交回复
热议问题