0

我希望更改替换字符串中的书写光标(键入时显示/隐藏光标)位置。

这是我的字符串:

if(str.indexOf('<html>') != -1){
    str = str.replace(/\<html\>([a-z|A-Z|0-9| ])/g, "<html>\n   \n</html>");
}

这意味着当我输入<html>结果时:

<html>

</html>|
       ^
The typing cursor

现在想要的结果:

<html>
|
</html>

如何在替换字符串中自定义/放置键入光标?

4

1 回答 1

0

单击 [Ask Question] 后,在 stackoverflow 上的 JS 控制台中尝试此操作:

document.querySelector('textarea#wmd-input').value = "Here >< is your caret"
document.querySelector('textarea#wmd-input').selectionStart = 6
document.querySelector('textarea#wmd-input').selectionEnd = 6

对于 textarea 以外的一些元素类型

element.setSelection(startPosition, startPosition)

可能会奏效。

于 2013-09-03T21:49:12.053 回答