JS - how to remove image with JavaScript?

前端 未结 9 1382
孤独总比滥情好
孤独总比滥情好 2021-02-13 11:27

I faced a problem as to remove image with JS code like a...


...
document.getElementById(\'image_X\').src=\'\'
         


        
9条回答
  •  梦毁少年i
    2021-02-13 12:15

    This should do it:

    var el = document.getElementById('image_X');
    el.parentNode.removeChild(el);
    

    You can try it here.

    You can neatly wrap that into a function like so:

    function removeElement(ele) {
        ele.parentNode.removeChild(ele);
    }
    
    removeElement(document.getElementById("image_X"));
    

提交回复
热议问题