如何将“焦点”设置回到可疑的div

时间:2022-08-26 20:23:18

Working in Windows 8 (IE10 engine). No need for compat with other browsers. I have a contenteditable div with text in it. User clicks another element on the page and I want to paste some HTML into the div where the user's cursor is and then return focus back to the div. I can paste into the div, but then the user can't continue typing and I can't find a way to return the focus. Anyone?

在Windows 8(IE10引擎)中工作。无需与其他浏览器兼容。我有一个带有文字的contenteditable div。用户单击页面上的另一个元素,我想将一些HTML粘贴到用户光标所在的div中,然后将焦点返回到div。我可以粘贴到div中,但用户无法继续键入,我找不到返回焦点的方法。任何人?

Here's the JavaScript I'm running when the other element is clicked...

这是我点击其他元素时运行的JavaScript ...

var sel = document.selection.createRange();
sel.pasteHTML(span.outerHTML);

(I did find similar questions but none were applicable)

(我确实找到了类似的问题,但没有一个适用)

1 个解决方案

#1


2  

Just use elem.focus();. It should work as normal. To move the cursor to the correct location, use setSelectionRange.

只需使用elem.focus();.它应该正常工作。要将光标移动到正确的位置,请使用setSelectionRange。

#1


2  

Just use elem.focus();. It should work as normal. To move the cursor to the correct location, use setSelectionRange.

只需使用elem.focus();.它应该正常工作。要将光标移动到正确的位置,请使用setSelectionRange。