[英]Using jQuery to remove a second form element without losing the child elements
我有一些AJAX工作带来了我在DOM中不需要的额外表单元素。 我没有能力删除它的服务器端,所以我正在寻找一些jQuery或经典JavaScript,它将允许我捕获表单中的所有子元素,删除我不需要的表单,最后重新将子元素追加回DOM。
任何帮助将非常感激
编辑:感谢您的快速解答,以下是我实际使用的,它的工作完美
// Captures the children
var children = $("form:eq(1)").children();
// Removes the form
$("form:eq(1)").remove();
// append the child elements back into the DOM
$("#fillDiv").append(children);
您应该移动子节点,而不是使用.html()
方法(这将导致您丢失与表单元素关联的任何事件):
// grab the child nodes
var children = $('#badForm').children();
// or var children = $('#badForm > *');
// move them to the body
$(body).append(children);
// remove the form
$('#badForm').remove();
如果你有这样的ajax代码:
$.ajax({
type: "get",
url: "url",
data: ,
cache: false,
success: function(data){
}
});
您可以添加success
功能:
$(data).find('#whatYouNeed').appendTo('#whereYouNeed')
通过这种方式,您可以从该页面中提取任何内容:)
你的问题仍然存在,但是现在有更好的答案。
unwrap
方法: http : //api.jquery.com/unwrap/ $element.replaceWith($element.contents());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.