简体   繁体   English

如何获得内容可编辑的插入符号位置

[英]How can I get the caret position on a contenteditable <button>

So I have a contenteditable button in my webpage I declare like so: 所以我在网页中声明了一个contenteditable按钮,如下所示:

<button id="test" contentEditable="true">This <img src="image..png" /> is a button, click me!</button>

I move the caret around, and I want to find its final location. 我四处移动插入符号,我想找到它的最终位置。 I tried both: 我都尝试过:

  • window.getSelection().focusOffset.toString() window.getSelection()。focusOffset.toString()
  • document.getElementById('test').selectionStart.toString()"; document.getElementById('test')。selectionStart.toString()“;

but neither of them seems to work :( Any pointers? 但它们似乎都不起作用:(有指针吗?

Update 更新资料

Unfortunately the code from the links below seems to work only with Firefox and not with Chrome (for a button , div s work just fine) 不幸的是,以下链接中的代码似乎仅适用于Firefox,不适用于Chrome(对于buttondiv正常工作)


Take a look at these: 看看这些:

Here's a quick and dirty fiddle - it isn't perfect but it might help you get started. 这是一个快速而肮脏的小提琴 -虽然并不完美,但可能会帮助您入门。 I've used code from the blog post at the second link above. 我使用了上面第二个链接中博客文章中的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 contenteditable 获得“删除插入符号位置”? - How can I get the "drop caret position" from contenteditable? 获取内容Editable caret position - Get contentEditable caret position 使用contentEditable时,如何使用Javascript获取插入符号元素? - How can I get the element the caret is in with Javascript, when using contentEditable? 如何检查{}中是否包含可编辑的插入符号 - How can I check if contenteditable caret is inside { } 如何获得包含图像的contenteditable div的插入位置 - how to get the caret position of a contenteditable div which contains images 如何获取 ContentEditable 元素中关于innerText 的插入符号位置? - How to get the caret position in ContentEditable elements with respect to the innerText? 如何在 contentEditable div 中获取插入符号 x 和 y 位置 - How to get caret x and y position in contentEditable div 如何在 contenteditable 中获取和设置插入符号位置? - How do you get and set the caret position in a contenteditable? 如何使用contentEditable从iframe中的当前插入位置获取像素偏移量 - How to get the pixel offset from the current caret position in an iframe with contentEditable 如何使用 html 子元素在 contenteditable div 中获取插入符号 position? - How to get caret position within contenteditable div with html child elements?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM