我正在尝试将 div 中的内容导出为 .txt 文件。但是所有段落都是并排呈现的。
例如,我在一个 div 内容中有如下行:
<div id="content>
<p>hello</p>
<p>How are you</p>
</div>
我想在 txt 文件中得到如下输出
hello
How are you
但是我得到的输出是
hello How are you
这是 jsfiddle 的链接: https://jsfiddle.net/j046xfoy/
最佳答案
尝试使用 .textContent
而不是 .innerText
:
.innerText
显示文本在 HTML 中的外观.textContent
将文本显示为原始textNodes
查看下面代码段中的区别:
片段
/* Styles are for demonstration */
#text {
border:1px solid blue;
float:right;
padding:0 10px;
}
.as-console-wrapper {
min-height:100%;
max-width:55%;
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div id="text">
<p>Ying
and Yang,</p>
<p style="visibility:hidden">Black and White</p>
<p>Evil and Good</p>
</div>
<script>
var text = document.getElementById('text');
var iText = text.innerText;
var tContent = text.textContent;
console.log('innerText: \n' + iText);
console.log('textContent: \n' + tContent);
</script>
</body>
</html>
关于javascript - 导出为 .txt 并排显示所有 <p> 行,而不是逐行显示 javascript jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41977019/