designMode iFrame 获取光标位置
我想获取可编辑 iFrame 的光标位置(使用 designMode)。这是我到目前为止的代码:
document.getElementById('iframe_id').contentWindow.document.getSelection().getRangeAt(0)
从那里获取属性 startOffset
获取从该行开头开始的字符数,但不是从 iFrame 文档开头开始的字符数。我想获取相对于文档开头的光标位置。
请注意:我不对设置光标位置感兴趣;我只是想得到它。
最好,我希望修复程序能够与 Chrome/Safari/Firefox 兼容;无需与 IE 兼容。
任何帮助将不胜感激。
I want to get the cursor position of an editable iFrame (using designMode). Here is the code I have so far:
document.getElementById('iframe_id').contentWindow.document.getSelection().getRangeAt(0)
From there, getting the property startOffset
gets the number of characters from the beginning of that line, but not from the beginning of the iFrame document. I would like to get the cursor position relative to the beginning of the document.
Please note: I am not interested in setting the cursor position; I just want to get it.
Preferably, I would like the fix to be compatible with Chrome/Safari/Firefox; compatibility with IE is not necessary.
Any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下内容基于此答案,但经过修改以适用于任何文档中的选择(例如 iframe 中的选择)。该答案中提出的关于这种方法天真的警告仍然适用。
用法示例:
The following is based on this answer but is modified to work with selections in any document (such as one in an iframe). The same caveats about the naïveté of this approach laid out in that answer still apply.
Example usage:
这对我有用
}
This worked for me
}