jQuery从html字符串中删除html标签 [英] jquery Remove html tags from html string

查看:519
本文介绍了jQuery从html字符串中删除html标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆