gpt4 book ai didi

javascript - 如何从TinyMCE获取高亮文本?

转载 作者:行者123 更新时间:2023-12-03 02:19:32 26 4
gpt4 key购买 nike

我想使用 TinyMCE 提取用户突出显示的文本。我已经能够使用 TinyMCE API 提取整个文本,甚至只提取使用 getNode() 选择的段落。我认为 getSel() 可以做到这一点,但它返回一个对象,而我想要字符串。

var content = tinymce.activeEditor.selection.getSel();
console.log(content);

返回:

Selection {anchorNode: text, anchorOffset: 259, focusNode: text, focusOffset: 286, isCollapsed: false, …}

TinyMCE:https://www.tinymce.com/docs/api/tinymce.dom/tinymce.dom.selection/#getsel

我还在 JavaScript 中发现了 getSelection,但它似乎在 Chrome 中无法正常工作。不管怎样,如果可能的话,我更喜欢使用 TinyMCE API。 https://developer.mozilla.org/en-US/docs/Web/API/Window/getSelection

最佳答案

基于官方文档 tinymce.com/docs/api/tinymce.dom/tinymce.dom.selection/#getcontent :

// Alerts the currently selected contents
alert(tinymce.activeEditor.selection.getContent());

// Alerts the currently selected contents as plain text
alert(tinymce.activeEditor.selection.getContent({format: 'text'}));

您可以这样编写代码:

var content = tinymce.activeEditor.selection.getContent();
console.log(content);

[编辑]或者获取纯文本内容:

var content = tinymce.activeEditor.selection.getContent({format: 'text'});
console.log(content);

注意 tinymce.activeEditor.selection.getContent() 之间的区别和 tinymce.activeEditor.getContent() .

  • tinymce.activeEditor.selection.getContent() - 获取编辑器中选定/突出显示内容的 HTML 或纯文本/textarea .

  • tinymce.activeEditor.getContent() - 获取编辑器的全部内容/textarea .

关于javascript - 如何从TinyMCE获取高亮文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49220722/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com