javascript 从 html 字符串中删除 div 及其内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16940274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove div and its content from html string
提问by Koiski
Lets say i have a string like this:
假设我有一个这样的字符串:
<div id="div1"></div>
<div class="aClass" id="div2">
<div id="div3" class="anotherClass"></div>
<div id="div4" />
</div>
<div id="div5"></div>
I want to remove div2 from the string and everything inside that div
我想从字符串中删除 div2 以及该 div 中的所有内容
So i got a string like this
所以我得到了这样的字符串
<div id="div1"></div>
<div id="div5"></div>
I thinking something like using regex to find the first div with the id of "div2" or whatever the id of the div is and count brackets untill it gets to "< /div>". The problem is that the "div3" also got a "< /div>" at the end.
我想像使用正则表达式来查找第一个 div 的 id 为“div2”或任何 div 的 id 并计算括号,直到它到达“< / div>”。问题是“div3”最后也有一个“</div>”。
The content of the div i want to remove may contain more or less div's then this too.
我想删除的 div 的内容可能包含更多或更少的 div,那么这个也是。
Any ideas on how to code this?
关于如何编码的任何想法?
Update:
更新:
var htmlText = editor3.getValue();
var jHtmlObject = jQuery(htmlText);
jHtmlObject.find("#div2").remove();
var newHtml = jHtmlObject.html();
console.log(newHtml);
Why doesn't this return anything in the console?
为什么这不会在控制台中返回任何内容?
Update2!: I have made a jsFiddle to make my problem visual.. http://jsfiddle.net/WGXHS/
更新 2!:我制作了一个 jsFiddle 来使我的问题可视化.. http://jsfiddle.net/WGXHS/
回答by Patrick Evans
Just put the string into jQuery and use find and then remove.
只需将字符串放入 jQuery 并使用查找然后删除。
var htmlString = '<div id="div1"></div>\
<div class="aClass" id="div2">\
<div id="div3" class="anotherClass"></div>\
<div id="div4" />\
</div>\
<div id="div5"></div>';
var jHtmlObject = jQuery(htmlString);
var editor = jQuery("<p>").append(jHtmlObject);
editor.find("#div2").remove();
var newHtml = editor.html();
回答by Jude Duran
jQuery .remove()
will do
jQuery.remove()
会做
$("#div2").remove();
回答by itsmejodie
If you have access to jQuery and your HTML is part of the DOM you can use $.remove()
如果您可以访问 jQuery 并且您的 HTML 是 DOM 的一部分,您可以使用$.remove()
EG. $('#div2').remove();
例如。 $('#div2').remove();
If it's not part of the DOM, and you have it in a string, you can do something like:
如果它不是 DOM 的一部分,并且您将它放在一个字符串中,您可以执行以下操作:
$('#div2', $(myHTML)).remove();
$('#div2', $(myHTML)).remove();
回答by Joe
The regex option would work if you control generating the string so you can ensure things like order of the attributes and indentation. If not your best bet is to use an HTML parser. If you are working inside of a browser jQuery is a good option. If you are working server-side you'll need to find a parser for the language you chose.
如果您控制生成字符串,则 regex 选项将起作用,因此您可以确保诸如属性和缩进的顺序之类的事情。如果不是,您最好的选择是使用 HTML 解析器。如果您在浏览器中工作,jQuery 是一个不错的选择。如果您在服务器端工作,则需要为您选择的语言找到一个解析器。