【问题标题】:Why window.getSelection () is different between firefox and chrome?为什么firefox和chrome的window.getSelection()不一样?
【发布时间】:2017-10-17 05:25:38
【问题描述】:

与:

<div contenteditable="true"> ... text.... <img src="image.png" height="50" width="50" onmouseup="alert(window.getSelection().isCollapsed)"> ... text... </div>

如果我在 firefox 警报中单击(img 上的鼠标)显示:'false', 在 chrome 警报中说:'true'。 为什么不一样? (火狐好像提示有选中的文字)

与:

<div contenteditable="true"> ... text.... <img src="image.png" alt="hello" height="50" width="50" onmouseup="alert(window.getSelection().toString())"> ... text... </div>

如果我在 Firefox 警报中单击(鼠标在 img 上)说:“你好”, 在 chrome 警报中说:''。 为什么 Firefox 说我选择了“你好”文本?火狐错了,chrome没有?

【问题讨论】:

    标签: javascript google-chrome firefox


    【解决方案1】:

    document/window.getSelection 方法在 Google Chrome、Safari 和 Internet Explorer 中的工作方式与在 Firefox 和 Opera 中不同。它在 Firefox 和 Opera 中返回一个字符串,在 Google Chrome、Safari 和 Internet Explorer 中返回一个 selectionRange 对象(document.getSelection 方法与 Google Chrome、Safari 和 Internet Explorer 中的 window.getSelection 方法相同)。因此,不应使用此方法。

    【讨论】:

    • 在我的电脑中,document/window.getSelection 方法也在 Firefox 中返回一个对象(anchorNode、anchorOffset 等):'alert(window.getSelection().isCollapsed)' 说 'false';如果它是一个字符串,带有 '.isCollapsed' 的 'window.getSelection()' 会说 'undefined'...
    • @trewius 所以你必须根据你正在使用的浏览器做一个条件。但无论如何我不推荐使用这种方法。
    • @TheUnnamed 你能推荐其他解决方案替换 window.getSelection 吗?谢谢。
    猜你喜欢
    • 1970-01-01
    • 2013-03-23
    • 2016-04-19
    • 1970-01-01
    • 2017-06-19
    • 2015-10-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-16
    相关资源
    最近更新 更多