我有一个按钮,当点击它时,我想在编辑器中添加一些文本。
我看到了一些问题,人们给出了解决办法,我试过了。但是,当我使用restoreRange和execCmd以及insertHTML时,它不会插入插入符号,只有在编辑器之前输入了某些内容,比如字符或空格。
换句话说,当单击编辑器时,它不会插入我单击时插入插入符号的位置,而是在编写某些内容时插入插入符号。
比如restoreRange只在写东西的时候起作用。
问题发生在以下代码中:
$('#editor').trumbowyg({
btnsDef: {
testButton: {
fn: function () {
// Restore the previous position
$("#editor").trumbowyg('restoreRange');
// Add text in the current position
$("#editor").trumbowyg('execCmd',
{
cmd: 'insertHTML',
param: 'Dummy text',
forceCss: false
});
},
title: 'test button',
text: 'insert text',
hasIcon: false
}
},
btns: [
['testButton'],
]
});在这里重现问题:https://jsfiddle.net/95nqv076/
我是不是遗漏了什么?
发布于 2020-08-19 08:40:29
找到解决方案:在模糊和焦点上保存范围。
$('#editor').trumbowyg({
btnsDef: {
testButton: {
fn: function () {
$("#editor").trumbowyg('restoreRange');
$("#editor").trumbowyg('execCmd',
{
cmd: 'insertHTML',
param: 'Dummy text',
forceCss: false
});
},
title: 'Button tooltip',
text: 'Displayed button name',
hasIcon: false
}
},
btns: [
['testButton'],
]
}).on('tbwblur', function(){
$("#editor").trumbowyg('saveRange');
}).on('tbwfocus', function(){
$("#editor").trumbowyg('saveRange');
});希望它能帮到别人!
https://stackoverflow.com/questions/63460918
复制相似问题