Javascript 当以编程方式更改contenteditable div的innerHTML时,Chrome GetRange出现问题

Javascript 当以编程方式更改contenteditable div的innerHTML时,Chrome GetRange出现问题,javascript,html,google-chrome,Javascript,Html,Google Chrome,我的html如下所示: <button onclick="testfunction()">test</button> <div id="divT" spellcheck="false" contenteditable="true"></div> 在Google chrome中,当我尝试像上面代码中那样以编程方式更改该div的内容,然后运行testfunction时,我

我的html如下所示:

<button onclick="testfunction()">test</button>
<div id="divT" spellcheck="false" contenteditable="true"></div>
在Google chrome中,当我尝试像上面代码中那样以编程方式更改该div的内容,然后运行
testfunction
时,我得到一个:
未能对“选择”执行“getRangeAt”:0不是有效索引

但是如果我手动更改内容,就不会有这样的问题。Firefox工作正常

这是虫子吗?提前感谢您的帮助

jsfiddle: 只需单击测试按钮

我就这样解决了它:

document.getElementById(“divT”).focus()

document.getElementById("divT").innerHTML = "bla bla bla";

function testfunction() {
    try {
        if (window.getSelection && (sel = window.getSelection()).modify) {
            var s = window.getSelection();
            var range = s.getRangeAt(0);
            range.collapse(true);
        }

    }
    catch(err) {
        alert(err);
    }
}