将 Range 或 DocumentFragment 转换为字符串
有没有办法在 W3C 兼容的浏览器中获取 JavaScript Range 对象的 html 字符串?
例如,假设用户选择以下内容:Hello World
可以使用 Range.toString()
方法以字符串形式获取“Hello World”。 (在 Firefox 中,也可以使用 document 的 getSelection
方法。)
但我似乎找不到获取内部 HTML 的方法。
经过一番搜索,我发现范围可以转换为 DocumentFragment
对象。
但 DocumentFragments
没有 innerHTML
属性(至少在 Firefox 中;尚未尝试过 Webkit 或 Opera)。
这对我来说似乎很奇怪:显然应该有某种方法来访问所选项目。
我意识到我可以创建一个 documentFragment
,将文档片段附加到另一个元素,然后获取该元素的 innerHTML
。
但该方法将自动关闭我选择的区域内所有打开的标签。
除此之外,肯定有一个明显的“更好的方法”,而不是将其附加到 dom 只是为了将其作为字符串获取。
那么,如何获取Range或DocFrag的html字符串呢?
Is there a way to get the html string of a JavaScript Range Object in W3C compliant browsers?
For example, let us say the user selects the following: Hello <b>World</b>
It is possible to get "Hello World" as a string using the Range.toString()
method.
(In Firefox, it is also possible using the document's getSelection
method.)
But I can't seem to find a way to get the inner HTML.
After some searching, I've found that the range can be converted to a DocumentFragment
Object.
But DocumentFragments
have no innerHTML
property (at least in Firefox; have not tried Webkit or Opera).
Which seems odd to me: It would seem obvious that there should be some way to acces the selected items.
I realize that I can create a documentFragment
, append the document fragment to another element, and then get the innerHTML
of that element.
But that method will auto close any open tags within the area I select.
Besides that there surely is an obvious "better way" than attaching it to the dom just to get it as a string.
So, how to get the string of the html of a Range or DocFrag?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
与其他响应相反,可以使用
XMLSerializer.prototype.serializeToString< 将
DocumentFragment
对象直接转换为DOMString
/code> 方法描述于 https://w3c.github.io/DOM-解析/#the-xmlserializer-interface。要获取
Range
对象的DOMString
,只需使用Range.prototype.cloneContentsDocumentFragment
code> 或 Range.prototype.extractContents 方法,然后按照DocumentFragment
对象的过程进行操作。我附上了一个演示,但其要点在于这两行:
Contrary to the other responses, it is possible to directly turn a
DocumentFragment
object into aDOMString
using theXMLSerializer.prototype.serializeToString
method described at https://w3c.github.io/DOM-Parsing/#the-xmlserializer-interface.To get the
DOMString
of aRange
object, simply convert it to aDocumentFragment
using either of theRange.prototype.cloneContents
orRange.prototype.extractContents
methods and then follow the procedure for aDocumentFragment
object.I've attached a demo, but the gist of it is in these two lines:
FWIW,jQuery 方式:
FWIW, the jQuery way:
详细说明此处的示例:
To spell out an example from here:
另一种方法是迭代 childNodes:
对于内联 SVG 不起作用,但可以采取一些措施让它工作。如果您需要对节点进行一些链式操作并获得 html 字符串作为结果,它也会有所帮助。
Another way to do it would be to iterate over childNodes:
Wouldn't work for inlined SVG, but something could be done to get it to work. It also helps if you need to do some chained manipulation with the nodes and get an html string as a result.
不,这是唯一的方法。大约 10 年前的 DOM Level 2 规范在节点与 HTML 文本之间的序列化和反序列化方面几乎没有任何内容,因此您被迫依赖像
innerHTML
这样的扩展。关于你的评论
……不然还能怎么用呢? DOM 由排列在树中的节点组成。从 DOM 复制内容只能创建另一个节点树。在 HTML 中,元素节点由开始标记(有时也由结束标记)分隔。需要结束标记的元素的 HTML 表示形式必须具有结束标记,否则它不是有效的 HTML。
No, that is the only way of doing it. The DOM Level 2 specs from around 10 years ago had almost nothing in terms of serializing and deserializing nodes to and from HTML text, so you're forced to rely on extensions like
innerHTML
.Regarding your comment that
... how else could it work? The DOM is made up of nodes arranged in a tree. Copying content from the DOM can only create another tree of nodes. Element nodes are delimited in HTML by a start and sometimes an end tag. An HTML representation of an element that requires an end tag must have an end tag, otherwise it is not valid HTML.
DocumentFragment.textContent 能否满足您的需求?
Could DocumentFragment.textContent give you what you need?