Cloning an element with file input child and emptying it

心不动则不痛 提交于 2019-12-11 17:13:42

问题


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

回答1:


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.

Assuming the HTML:

<div class="wrap"><input type="file" /></div>
<span id="add-more-files">Add more</span>

You can do:

$('#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
});

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.

Note: It doesn't insert a new input into the same div. If that's what you wanted see @jAX's answer.




回答2:


$('#add-more-files').click(function()
{
   $('<input type="file" />').insertBefore($(this));
});


来源:https://stackoverflow.com/questions/5598954/cloning-an-element-with-file-input-child-and-emptying-it

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!