我有这样的 HTML:
<span class="file-wrapper" id="fileSpan">
<input type="file" name="photo[]" id="photo" />
<span class="button">Click to choose photo</span>
</span>
我想从那里提取输入字段,更改它的 ID 并将它放在另一个 div 中。
我该怎么做?如果需要 jQuery 没关系,但如果不需要 jQuery 那就太好了。
最佳答案
在 jQuery 中这当然很容易:
// jQuery 1.6+
$("#photo").prop("id", "newId").appendTo("#someOtherDiv");
// jQuery (all versions)
$("#photo").attr("id", "newId").appendTo("#someOtherDiv");
Working demo: http://jsfiddle.net/AndyE/a93Az/
如果你想用普通的 ol' JS 来做,它仍然相当简单:
var photo = document.getElementById("photo");
photo.id = "newId";
document.getElementById("someOtherDiv").appendChild(photo);
Working demo: http://jsfiddle.net/AndyE/a93Az/1/
关于javascript - 更改元素 ID 后将元素移动到另一个父元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7555442/