best way to write jQuery's replaceWith() in natural JavaScript

后端 未结 1 453
陌清茗
陌清茗 2020-12-15 13:33

Having trouble with a function hitting the page as soon as possible, so i need to write it in pure javascript and include it in the head. not sure how to go about it, becaus

相关标签:
1条回答
  • 2020-12-15 13:50
    var image = document.getElementById('imagefiles'), parent = image.parentNode,
    tempDiv = document.createElement('div');
    
    tempDiv.innerHTML = "<input type='file' name='imagefiles' id='imagefiles' />"
    
    var input = tempDiv.childNodes[0];
    
    parent.replaceChild(input, image);
    

    DEMO


    EDIT as per am not i am:

    var image = document.getElementById('imagefiles'), parent = image.parentNode,
    input = document.createElement('input');
    input.id = input.name = "imagefiles";
    input.type = 'file';
    
    parent.replaceChild(input, image);
    

    edited DEMO

    0 讨论(0)
提交回复
热议问题