简体   繁体   English

Javascript - 从元素中删除图像

[英]Javascript - Remove Image from element

I am trying to remove the image from the following html, but am running into trouble.我正在尝试从以下 html 中删除图像,但遇到了麻烦。 I am able to specify the ID is java script but am not sure how to remove the associated img.我可以指定 ID 为 java 脚本,但不确定如何删除关联的 img。

<a href="https://google.com" target="_blank" id="my_banner_image" style="display: block;">
    <img width="970" height="250" src="https://via.placeholder.com/970x250.png" boarder="0">
</a>

I have tried using, but am not quite sure how to specify the img.我尝试过使用,但不太确定如何指定 img。

var element = document.getElementById("my_banner_image");
    element.parentNode.removeChild(image);

Locate img element using querySelector method:使用querySelector方法定位 img 元素:

document.getElementById("my_banner_image")
    .querySelector('img')
    .remove()

It will remove the first img tag inside element with id my_banner_image它将删除 id my_banner_image的元素内的第一个 img 标签

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM