Chrome / Safari中的ContentEditable焦点 [英] ContentEditable focus in Chrome/Safari
问题描述
document.getElementById(我们有一个contenteditable div需要关注页面加载(将光标放在第一行)编辑器)。焦点()
在Firefox和IE中运行良好,但是在Chrome / Safari中它只是选择整个内容! 一种方法使这项工作正常?
预先感谢,
Shesh
这个问题可能不再与提问者有关,但我会为未来的读者回答。基本原则是在元素开始处将选择设置为折叠范围。
我使用Rangy JS库解决了这个问题: http://code.google.com/p/rangy
var r = rangy.createRange();
r.setStart(document.getElementById('editor'),0);
r.collapse(true);
rangy.getSelection()。setSingleRange(r);
这可能不仅仅是必要的,但它绝对可以跨浏览器使用,并且Rangy提供了一堆额外的公用事业以及它可能值得研究:)
I have a contenteditable div which needs to be focussed at pageload (place cursor at the first line).
document.getElementById("editor").focus()
works well in Firefox and IE, but in Chrome/Safari it just selects the entire content!
Is there a way to make this work properly?
Thanks in advance,
Shesh
This question may no longer be relevant to the questioner but I'll answer it for future readers. The basic principle is to set the selection to a collapsed range at the start of the element.
I solved this problem by using the Rangy JS library: http://code.google.com/p/rangy
var r = rangy.createRange();
r.setStart( document.getElementById('editor'), 0 );
r.collapse(true);
rangy.getSelection().setSingleRange(r);
This might be more than is necessary but it definitely works cross-browser and Rangy provides a bunch of extra utilities as well so it's probably worth looking into :)
这篇关于Chrome / Safari中的ContentEditable焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!