Rangy & ContentEditable - Set the Caret

一个人想着一个人 提交于 2019-12-10 10:17:24

问题


I'm trying out the latest version of the "rangy" jQuery plugin (1.2 beta) to set the caret in a contenteditable DIV with a specific offset.

However, it responds with a weird error in Firefox: Security error" code: "1000

Here is the offending code:

var el = $("#editablediv"), index = 11;
var range = rangy.createRange();
range.setStart(el, index);
var sel = rangy.getSelection();
sel.setSingleRange(range);

The code fails when calling the setStart function.

Could anyone give an example of the proper usage of rangy please?


回答1:


I found the issue, I was supposed to pass through the correct node which is the text node:

var el = $("#editablediv"), index = 11;
var range = rangy.createRange();
range.setStart(el[0].childNodes[0], index);
range.collapse(true);
var sel = rangy.getSelection();
sel.setSingleRange(range);


来源:https://stackoverflow.com/questions/6816078/rangy-contenteditable-set-the-caret

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!