简体   繁体   English

HTML RTF文本编辑:在选择内容周围包装自定义html

[英]HTML Rich-text editing: wrap custom html around selection

I am using html 5 rich text editing via the jWysiwyg library. 我正在通过jWysiwyg库使用html 5 RTF编辑。 I want to be able to surround the selected part of the document with custom HTML, much like how one would highlight text on the document and mark it as bold. 我希望能够用自定义HTML包围文档的选定部分,就像人们在文档上突出显示文本并将其标记为粗体一样。 There is very limited information on the subject, and I'm not even sure if it's possible. 关于该主题的信息非常有限,我甚至不确定是否可行。 I've also tried to get the raw html selection of the page, but so far I've only been able to get a plain-text version of whatever is highlighted on the screen. 我也尝试获取页面的原始html选择,但到目前为止,我只能获得屏幕上突出显示的内容的纯文本版本。 Any recommendations would be much appreciated. 任何建议将不胜感激。

After further research, I discovered that the getInternalRange() function in jWysiwyg effectively returns the object generated by document.createRange(). 经过进一步研究,我发现jWysiwyg中的getInternalRange()函数有效地返回了由document.createRange()生成的对象。 After reviewing the documentation, I was able to locate the surroundContents() method, which meets my needs. 阅读文档后,我能够找到满足我需求的SurroundContents()方法。 An example is provided below. 下面提供一个示例。 I'm using jQuery, so I will also demonstrate how to use jQuery to generate the html element for you: 我正在使用jQuery,因此我还将演示如何使用jQuery为您生成html元素:

var range = document.createRange();

range.surroundContents($('<span style="background-color: red;" />').get(0));

This particular example highlights the selected text in red, however this example can be easily generalized to allow a developer to surround the selection with any number of html elements. 这个特定的示例以红色突出显示了选定的文本,但是可以轻松地概括该示例,以允许开发人员将选择内容周围包含任意数量的html元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM