如何从jquery对象中删除dom元素
我想从我的对象中删除一个特定的类,因为我的要求是在显示内容之前删除该dom数据。 我已经编写了一个示例代码但无法理解为什么它不起作用。 我jquery的删除也无法正常工作。 请帮我解决。 提前致谢
test $(document).ready(function() { // complete html var test; test = $('#issue_detail_first_row').html(); var x = $(test).find('#issue_detail_nav').not('.p1'); $('#sett').html(x); });
您需要直接从DOM中删除内容。
$("#issue_detail_first_row .p1").remove();
这将选择.p1
元素并从DOM中删除它们
你可以在javascript对象上使用删除function。
如果要在显示之前预处理它。
例
var a =$("#issue_detail_first_row").html(); var jhtml =$(a)[0]; $(jhtml).find('.p1').remove(); alert($(jhtml).html());
现在使用jhtml。 演示
您似乎正在尝试复制一个部分,但没有.p1
元素。
您可以使用clone()
[docs]方法克隆该部分,使用remove()
[docs]方法删除您不想要的内容,然后插入其HTML。
$(document).ready(function() { var test = $('#issue_detail_first_row').clone(); // clone it test.find('.p1').remove(); // find and remove the class p1 elements $('#sett').html( test.html() ); // insert the new content });
工作示例: http : //jsfiddle.net/YDZ9U/1/
唯一的事情是你需要经历并更新克隆中的ID,以便它们不会在页面上重复。
Interesting Posts