【发布时间】:2014-08-28 10:57:33
【问题描述】:
我编写了一个文档编辑器,它使用 contentEditable 创建 HTML 内容。在一些较大的文档中,语法风格似乎无处不在。这很可能是从写字板和早期版本的编辑器粘贴内容的结果。
问题是,现在我留下了很多非常不一致的文档。
一开始还算正常。每行的简单<p>标签
<p>It is a truth</p>
<p>universally acknowledged</p>
<p>that a single man</p>
到目前为止,唯一“糟糕”的 html 是几个空的 <i></i> 标签,以及偶尔出现的 &nbsp; 而不是空格(有人知道为什么吗?)
然后它大约在文档的一半处,换行符切换到这种格式。
<div>
<br>
CHAPTER 1<br>
<br>
The sky above the port
<br>
was the color of a television
<br>
tuned to a dead channel.
</div>
<div>
<br>
</div>
然后在页面下方大约 3/4 处,我们得到了这个。它似乎已恢复为<p></p> 标签,但现在将它们随机嵌入<span> 标签中,lang 属性为空
<div>
<span lang="">
<p>It was the best of times,</p>
<p>it was the worst of times,</p>
</span>
<p>it was the age of wisdom,</p>
<p>it was the age of foolishness,</p>
</div>
注意:有些行在<span> 内,有些在外。
更糟糕的是,稍后我们会嵌套<span> 标签
<span lang="">
<div>
<span lang="EN-GB">
<p>Stately, plump </p>
<p>Buck Mulligan came </p>
<span lang="EN-GB">
<p>from the stairhead, </p>
<p>bearing a bowl of lather </p>
<span lang="EN-GB">
<p> on which a mirror and a razor lay crossed</p>
</span>
</span>
</span>
</div>
</span>
您可能还注意到 <span> 和 <div> 标签的父代现在一开始就颠倒了,<div> 现在是 <span> 的子代
我注意到了其他奇怪的事情。开始时使用<i></i>,但后来使用<em></em>。
- 清理此 HTML 的最佳方法是什么?
- 我应该尝试用
<p>标签包围孤立的行吗? - 如何只删除那些本身包含
<p>标签的<div>标签?以及如何避免在文档中留下孤立的文本?
【问题讨论】:
标签: c# javascript jquery html css