使用文件输入子级克隆元素并将其清空 [英] Cloning an element with file input child and emptying it
本文介绍了使用文件输入子级克隆元素并将其清空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请参考此问题,我需要一个解决方案:
Referring to this question I need a solution for it:
HTML:
<input type="file" />
<span id="add-more-files">Add more</span>
jQuery:
$('#add-more-files').click(function()
{
var cloned = $(this).prev().clone();
$(cloned).insertBefore($(this));
});
推荐答案
注意:您真的应该问一个新问题,不要只是复制您的旧问题,而要让人们四处寻找你想要什么.
Note: You really should ask a new question, don't just copy your old one and make people jump around to figure out what you want.
假设使用HTML:
<div class="wrap"><input type="file" /></div>
<span id="add-more-files">Add more</span>
您可以这样做:
$('#add-more-files').click(function(){
$("<div>", {class:"wrap"}) //Create a div
.append($("<input>", {type:'file'})) //Add an input element
.insertBefore(this); //Insert it into the DOM
});
这将创建一个新的div和输入标签结构,然后将其插入到可点击范围之前的DOM中.有关工作示例,请参见此处.
This creates a new div and input tag structure and then inserts it into the DOM before the clickable span. See here for a working example.
注意:它不会在同一个div中插入新的输入.如果那是您想要的,请参阅@jAX的答案.
Note: It doesn't insert a new input into the same div. If that's what you wanted see @jAX's answer.
这篇关于使用文件输入子级克隆元素并将其清空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文