var currentLineText = txt.split('\n')[cursorPos.row]; 我们没有插入'\n 这个代码是不是有问题?
时间: 2024-03-28 10:42:29 浏览: 25
非常抱歉,您说得对,我的回答有误。在代码`editor.cmd.do('insertHTML', '<br>')`中,并没有插入`\n`字符,因此`txt.split('\n')`并不是必须的。在这种情况下,我们可以通过`cursorPos.row`获取光标所在的行数,`cursorPos.column`获取光标所在的列数,通过判断光标是否在当前行的末尾来判断是否需要插入`<br>`标签。代码示例如下:
```javascript
editor.config.onkeydown = function (event) {
if (event.keyCode === 13 && event.shiftKey) {
var range = editor.selection.getRange();
var txt = editor.txt.text();
var cursorPos = range.start;
var currentLineText = txt.split('\n')[cursorPos.row];
var isCursorAtLineEnd = (cursorPos.column === currentLineText.length - 1);
if (!isCursorAtLineEnd) {
editor.cmd.do('insertHTML', '<br>');
}
return false;
}
}
```
此代码会在按下“Shift+Enter”键时,判断光标是否在当前行的末尾,如果不在末尾则插入换行符。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)