javascript - 右键单击文本字段是否应该像左键单击一样移动插入符?

标签 javascript html firefox selection

Firefox 的右击行为与 Chrome 和 IE 不同。当我右键单击文本字段时,插入符号不会移动。这是一个只适用于 firefox 的标准吗?这让我很沮丧。

我希望插入符号移动,然后我可以通过 element.selectionStart 知道它在哪里,然后找出用户选择的单词。除了firefox,其他浏览器都正常!

有没有人知道标准是什么或如何解决这个问题? Firefox 确实知道用户在右键单击时选择的确切单词,因为检查拼写有效。但我不知道。

我的代码:-

function onContextMenu(evt){
        var el = evt.getTarget(), selStart, r, ln = el.value.length;
        // Get the location of the cursor
        if (el.createTextRange) { // IE
            r = document.selection.createRange().duplicate();
            r.moveEnd('textedit');
            selStart = (r.text == '') ? ln : ln - r.text.length;
        } else { // All compliant browsers
            selStart = el.selectionStart;         //works fine for chrome
        }
}

描述问题的屏幕截图:-

enter image description here

最佳答案

好的。在 firefox 中,可以使用 event.rangeOffset。

关于javascript - 右键单击文本字段是否应该像左键单击一样移动插入符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10513477/

相关文章:

javascript - 防止 javascript 改变 JSON 的顺序

javascript - d3.js - 如何风格化饼图

css - Extjs 3.x : Default theme color in tabpanel tabs instead of white

javascript - jQueryUI选项卡和Firefox:getBBox损坏了吗?

javascript - 为 li 和 ul li 设置不同的 <span> 标签(嵌套)

html - 导航栏边框

html - "Span"选择器不工作

javascript - 将文件放入区域 - 样式

javascript - <br> 插入 contenteditable span 无法移动光标

javascript - 如何在方表中找到一个数字所属的一组数字的索引?