jQuery从html字符串中删除html标签 [英] jquery Remove html tags from html string
问题描述
我需要将html字符串从页面加载到内存中,并使用jQuery删除具有特定类的div.
I need to load an html string into memory from the page and remove divs that have a certain class using jQuery.
我想做的事情在下面,但不起作用.
What I am trying to do is below but it doesn't work.
var reportHTML = $('#collapsereportsdata').html()
$(reportHTML).(".dontprintme").each().remove();
谢谢
推荐答案
要获取已删除标签的HTML,您可以 .clone()
元素,并在获取HTML之前删除不需要的元素,如下所示:
To get the HTML with the tags removed you can .clone()
the element and remove the elements you don't want before getting it's HTML, like this:
var newHTML = $('#collapsereportsdata').clone().find(".dontprintme")
.remove().end().html();
这将执行原始元素的 .clone()
,并执行 .remove()
,然后使用 .end()
跳回到克隆的元素,因为那是您想要通过 .html()
来获取html的元素.
This performs a .clone()
of the original element, does a .find()
to get the elements you want to .remove()
, then uses .end()
to jump back to the cloned element, since that's the one you'd want to get the html via .html()
from.
这篇关于jQuery从html字符串中删除html标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!