2011-09-26 102 views
10

我有HTML這樣的:移動元素到另一個父

<span class="file-wrapper" id="fileSpan"> 
    <input type="file" name="photo[]" id="photo" /> 
    <span class="button">Click to choose photo</span> 
</span> 

我想從那裏提取輸入領域,改變它的ID,並把它在其他分區。

我該怎麼做?如果jQuery是需要的,那麼可以,但如果沒有,那會很好。

回答

19

這當然很容易在jQuery的:

// jQuery 1.6+ 
$("#photo").prop("id", "newId").appendTo("#someOtherDiv"); 

// jQuery (all versions) 
$("#photo").attr("id", "newId").appendTo("#someOtherDiv"); 

工作演示:http://jsfiddle.net/AndyE/a93Az/


如果你想這樣做的純醇」 JS,它仍然是相當簡單:

var photo = document.getElementById("photo"); 
photo.id = "newId"; 
document.getElementById("someOtherDiv").appendChild(photo); 

工作演示:http://jsfiddle.net/AndyE/a93Az/1/

+0

只有在1.6 + ... –

+0

@Royi:方便地移植到舊版本,只是改變'道具()''到ATTR()'。 –

+0

不應該在可能的時候選擇最新版本的jQuery嗎? – Dan

相關問題