ACE 编辑器双击后获取选中的单词
ACE editor get selected word after double click
当我双击一个词时,我想得到那个词。
editor = ace.edit("code_editor");
editor.on('dblclick', function() {
var selected_word = editor.getSelectedText()
});
我可以用上面的代码检测双击。但是,所选单词始终为空。它太快了,不允许选择一个完整的单词。这里的解决方案是什么?
你的判断是对的,太快了。我试图延迟选择抓取并且使用它工作正常:
editor.on("dblclick", function () {
setTimeout(() => {
var selected_word = editor.getSelectedText();
console.log(selected_word);
}, 10);
});
问题就解决了
var editor = ace.edit("editor");
editor.on("dblclick", function () {
setTimeout(() => {
var selected_word = editor.getSelectedText();
console.log(selected_word);
}, 10);
});
#editor {
position: absolute;
width: 500px;
height: 400px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Ace Editor</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.12/ace.js"></script>
</head>
<body>
<div id="editor">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Accusantium
expedita porro doloremque error, nobis dignissimos ducimus quidem. Quo
cumque pariatur debitis laborum, minus magnam enim, repudiandae omnis iste
cupiditate culpa.
</div>
</body>
</html>
Added working code snipet
当我双击一个词时,我想得到那个词。
editor = ace.edit("code_editor");
editor.on('dblclick', function() {
var selected_word = editor.getSelectedText()
});
我可以用上面的代码检测双击。但是,所选单词始终为空。它太快了,不允许选择一个完整的单词。这里的解决方案是什么?
你的判断是对的,太快了。我试图延迟选择抓取并且使用它工作正常:
editor.on("dblclick", function () {
setTimeout(() => {
var selected_word = editor.getSelectedText();
console.log(selected_word);
}, 10);
});
问题就解决了
var editor = ace.edit("editor");
editor.on("dblclick", function () {
setTimeout(() => {
var selected_word = editor.getSelectedText();
console.log(selected_word);
}, 10);
});
#editor {
position: absolute;
width: 500px;
height: 400px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Ace Editor</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.12/ace.js"></script>
</head>
<body>
<div id="editor">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Accusantium
expedita porro doloremque error, nobis dignissimos ducimus quidem. Quo
cumque pariatur debitis laborum, minus magnam enim, repudiandae omnis iste
cupiditate culpa.
</div>
</body>
</html>
Added working code snipet