我正在使用 CKEditor 作为我的 Python Flask 项目的一部分。我正在尝试将用鼠标选择的文本传递到另一个“文本区域”。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>CKEditor Örneği</title>
<script src="https://cdn.ckeditor.com/4.16.2/standard/ckeditor.js"></script>
</head>
<body>
<textarea id="editor1" name="editor1"></textarea>
<br>
<textarea id="selectedTextDisplay" name="selectedTextDisplay" readonly></textarea>
<script>
CKEDITOR.replace('editor1');
function showSelectedText() {
var editor = CKEDITOR.instances.editor1;
var selectedText = editor.getSelection().getSelectedText();
document.getElementById('selectedTextDisplay').value = selectedText;
}
CKEDITOR.instances.editor1.on('selectionChange', function (evt) {
showSelectedText();
});
</script>
</body>
</html>
我有上面的代码,问题是当我选择文本时,它不会直接将其传递到其他文本区域。我必须单击编辑器中的其他位置,或者在计算机上的选项卡或程序之间切换,当我返回网页时,就会发生复制过程。
我尝试过像这样使用“mouseup”,但没有帮助。
CKEDITOR.instances.editor1.on('mouse', function (evt) {
showSelectedText();
});
以下作品(说实话,我有点作弊)。
CKEDITOR.instances.editor1.on('contentDom', function () {
this.document.on('selectionchange', showSelectedText);
});
它的作用是:
DOMContentLoaded
,但在本例中它指的是 CKEditor 内容的加载 - 它实际上是创建一个 iframe,并用内容填充它。selectionchange
中缺少大写。这是因为我正在使用 Selection API中的
selectionchange
事件。可以在here找到一个可用的小提琴(很抱歉提供场外资源,但 stacksnippet 报告了 CORS 错误,我不知道如何克服它们)。