Javascript 是否删除所有仅保留HTML的web文本内容?
需要从html文件中删除所有web内容,只保留html标记 它可以通过正则表达式或JavaScript实现吗 之前:Javascript 是否删除所有仅保留HTML的web文本内容?,javascript,html,regex,Javascript,Html,Regex,需要从html文件中删除所有web内容,只保留html标记 它可以通过正则表达式或JavaScript实现吗 之前: <html> <head> <title>Ask a Question - Stack Overflow</title> <link rel="shortcut icon" href="//cdn.sstatic.net/stackoverflow/img/favicon.ico"> <script type="t
<html>
<head>
<title>Ask a Question - Stack Overflow</title>
<link rel="shortcut icon" href="//cdn.sstatic.net/stackoverflow/img/favicon.ico">
<script type="text/javascript">
document.write("Code remains un-touched");
</script>
</head>
<body class="ask-page new-topbar">
<div id="first">ONE</div>
<div id="sec">TWO</div>
<div id="third">THREE</div>
</body>
</html>
<html>
<head>
<title></title>
<link rel="shortcut icon" href="//cdn.sstatic.net/stackoverflow/img/favicon.ico">
<script type="text/javascript">
document.write("Code remains un-touched");
</script>
</head>
<body class="ask-page new-topbar">
<div id="first"></div>
<div id="sec"></div>
<div id="third"></div>
</body>
</html>
问一个问题-堆栈溢出
文件。写入(“代码保持未触及”);
一个
两个
三
之后:
<html>
<head>
<title>Ask a Question - Stack Overflow</title>
<link rel="shortcut icon" href="//cdn.sstatic.net/stackoverflow/img/favicon.ico">
<script type="text/javascript">
document.write("Code remains un-touched");
</script>
</head>
<body class="ask-page new-topbar">
<div id="first">ONE</div>
<div id="sec">TWO</div>
<div id="third">THREE</div>
</body>
</html>
<html>
<head>
<title></title>
<link rel="shortcut icon" href="//cdn.sstatic.net/stackoverflow/img/favicon.ico">
<script type="text/javascript">
document.write("Code remains un-touched");
</script>
</head>
<body class="ask-page new-topbar">
<div id="first"></div>
<div id="sec"></div>
<div id="third"></div>
</body>
</html>
文件。写入(“代码保持未触及”);
更新:需要使用更高版本的HTML标记,剥离web内容后,应显示HTML。最后,我对HTML代码很感兴趣。我想这样的代码应该可以:
$('*').each(function() {
$(this).contents().filter(function() {
return this.nodeType == 3 && this.parentNode.nodeName != 'SCRIPT';
}).remove();
});
(function removeTextNodes(el) {
Array.apply([], el.childNodes).forEach(function (child) {
if (child.nodeType === 3 && el.nodeName !== 'SCRIPT') {
// remove the text node
el.removeChild(child);
}
else if (child.nodeType === 1) {
// call recursive for child nodes
removeTextNodes(child);
}
});
})(document.documentElement);
迭代所有元素,查看它们的所有子节点,如果它们是文本节点而不在脚本中,则杀死它们
你可以在这页上测试:p
(Yoshi的jQueryless脚本速度更快,但写起来更短:p)
编辑:nodeName
是大写的。哎呀
编辑OP的编辑:这将随后获取源代码:
$('html')[0].outerHTML
您可以使用以下方式显示它:
$('body').text($('html')[0].outerHTML)
再次编辑:另外,如果您希望它没有jQueryless,您也可以改为使用document.documentElement.outerHTML
(这既快又好)。也适用于Yoshi的解决方案。我认为这样的解决方案应该可行:
$('*').each(function() {
$(this).contents().filter(function() {
return this.nodeType == 3 && this.parentNode.nodeName != 'SCRIPT';
}).remove();
});
(function removeTextNodes(el) {
Array.apply([], el.childNodes).forEach(function (child) {
if (child.nodeType === 3 && el.nodeName !== 'SCRIPT') {
// remove the text node
el.removeChild(child);
}
else if (child.nodeType === 1) {
// call recursive for child nodes
removeTextNodes(child);
}
});
})(document.documentElement);
迭代所有元素,查看它们的所有子节点,如果它们是文本节点而不在脚本中,则杀死它们
你可以在这页上测试:p
(Yoshi的jQueryless脚本速度更快,但写起来更短:p)
编辑:nodeName
是大写的。哎呀
编辑OP的编辑:这将随后获取源代码:
$('html')[0].outerHTML
您可以使用以下方式显示它:
$('body').text($('html')[0].outerHTML)
再次编辑:另外,如果您希望它没有jQueryless,您也可以改为使用document.documentElement.outerHTML
(这既快又好)。也适用于Yoshi的解决方案。一个简单的递归函数可以:
$('*').each(function() {
$(this).contents().filter(function() {
return this.nodeType == 3 && this.parentNode.nodeName != 'SCRIPT';
}).remove();
});
(function removeTextNodes(el) {
Array.apply([], el.childNodes).forEach(function (child) {
if (child.nodeType === 3 && el.nodeName !== 'SCRIPT') {
// remove the text node
el.removeChild(child);
}
else if (child.nodeType === 1) {
// call recursive for child nodes
removeTextNodes(child);
}
});
})(document.documentElement);
引用Amadan:只需使用document.documentElement.outerHTML
将html作为字符串获取。一个简单的递归函数即可:
$('*').each(function() {
$(this).contents().filter(function() {
return this.nodeType == 3 && this.parentNode.nodeName != 'SCRIPT';
}).remove();
});
(function removeTextNodes(el) {
Array.apply([], el.childNodes).forEach(function (child) {
if (child.nodeType === 3 && el.nodeName !== 'SCRIPT') {
// remove the text node
el.removeChild(child);
}
else if (child.nodeType === 1) {
// call recursive for child nodes
removeTextNodes(child);
}
});
})(document.documentElement);
引用Amadan:只需使用document.documentElement.outerHTML
将html作为字符串获取。您需要检查每个元素是否包含内容。如果是,请删除它,工具可以是任何内容。@j08691:无论如何,没有问题。您需要检查每个元素是否包含内容。如果是,请删除它,工具可以是任何内容。@j08691:无论如何,没问题。是的,它是正确的,但我想要的是html代码,而不是呈现的html!但它不适用于此HTML源代码:HTML被覆盖。HTML DOM应该是非接触式的。另外@Amadanyes这是正确的,但我想要的是html代码,而不是呈现的html!但它不适用于此HTML源代码:HTML被覆盖。HTML DOM应该是非接触式的。另外@Amadan看起来不错,让我在一些html上测试一下,关于需求,我会给你回复的。:)看起来不错,让我在一些html上测试一下,关于需求,我会给你回复的。:)