在自然JavaScript中编写jQuery的replaceWith()的最佳方法 [英] best way to write jQuery's replaceWith() in natural JavaScript

查看:133
本文介绍了在自然JavaScript中编写jQuery的replaceWith()的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个功能尽快点击页面的问题,所以我需要用纯JavaScript编写它并将其包含在头部。不确定如何去做,因为据我所知,通过使用.replace(),新元素将被移动到页面上的不同位置。 jQuery的replaceWith()行为是理想的。

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, because as i understand it, by using .replace() the new element will be moved to a different location on the page. jQuery's replaceWith() behavior is ideal.

$("#imagefiles").replaceWith("<input type='file' name='imagefiles' id='imagefiles' />");


推荐答案

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






按照我的编辑编辑:

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);

这篇关于在自然JavaScript中编写jQuery的replaceWith()的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆