Javascript 将插入符号移动到可编辑内容的末尾
我正面临一个恼人的问题。我正在编写一个小RTE,现在我正在尝试移动元素末尾的插入符号 现在,这是我(从这篇文章中)得到的代码: 我有一些自定义函数来处理范围和窗口。我几乎可以肯定他们是正确的Javascript 将插入符号移动到可编辑内容的末尾,javascript,contenteditable,chromium,caret,Javascript,Contenteditable,Chromium,Caret,我正面临一个恼人的问题。我正在编写一个小RTE,现在我正在尝试移动元素末尾的插入符号 现在,这是我(从这篇文章中)得到的代码: 我有一些自定义函数来处理范围和窗口。我几乎可以肯定他们是正确的 function(contentEditableElement){ var contentEditableElement = this.getFocusedElement().parentNode; if(this.contentDoc.createRange)//Firefox, Chro
function(contentEditableElement){
var contentEditableElement = this.getFocusedElement().parentNode;
if(this.contentDoc.createRange)//Firefox, Chrome, Opera, Safari, IE 9+
{
range = this.contentDoc.createRange();//Create a range (a range is a like the selection but invisible)
range.selectNodeContents(contentEditableElement);//Select the entire contents of the element with the range
range.collapse(false);//collapse the range to the end point. false means collapse to end rather than the start
selection = this.win.getSelection();//get the selection object (allows you to change selection)
selection.removeAllRanges();//remove any selections already made
selection.addRange(range);//make the range you have just created the visible selection
}
else if(this.doc.selection)//IE 8 and lower
{
range = document.body.createTextRange();//Create a range (a range is a like the selection but invisible)
range.moveToElementText(contentEditableElement);//Select the entire contents of the element with the range
range.collapse(false);//collapse the range to the end point. false means collapse to end rather than the start
range.select();//Select the range (make it the visible selection
}
}
它在FF和Opera下工作得很好…但在Chromium中,插入符号根本不移动
你能帮我找到正确的路吗?谢谢它适合我(Chrome 19)。看看这个JSFIDLE:我知道您有一个iframe,但它的行为应该是一样的。它在JSFIDLE上工作得很好,但在iframe上却不行。我在某个地方读到WebKit有一个bug,人们给出了一些解决方法。在我的Iframe中,如果我在元素的末尾添加一些字符(,如此),那么我可以在的末尾设置插入符号。如果它是空的,那么我不能。看来我要做一些缓冲了。是的,如果它是空的,它就不工作了。这是一个错误。我甚至认为这不是特定于WebKit的。