Javascript 为什么firefox和chrome的window.getSelection()不同?

Javascript 为什么firefox和chrome的window.getSelection()不同?,javascript,google-chrome,firefox,Javascript,Google Chrome,Firefox,与: 。。。文本。。。文本。。。 如果我在firefox警报中单击(img上的鼠标)会显示:“false”, 在chrome中,警告说:“真的”。 为什么不同?(Firefox似乎表明存在选中的文本) 与: 。。。文本。。。文本。。。 如果我在firefox警报中单击(img上的鼠标),就会显示:“你好”, 在chrome中,警报显示:“”。 为什么Firefox会说我选择了“hello”文本?Firefox错误,chrome否?谷歌chrome、Safari和Internet Explor

与:

。。。文本。。。文本。。。
如果我在firefox警报中单击(img上的鼠标)会显示:“false”, 在chrome中,警告说:“真的”。 为什么不同?(Firefox似乎表明存在选中的文本)

与:

。。。文本。。。文本。。。
如果我在firefox警报中单击(img上的鼠标),就会显示:“你好”, 在chrome中,警报显示:“”。
为什么Firefox会说我选择了“hello”文本?Firefox错误,chrome否?

谷歌chrome、Safari和Internet Explorer中的
文档/窗口。getSelection
方法与Firefox和Opera中的不同。它在Firefox和Opera中返回字符串,在Google Chrome、Safari和Internet Explorer中返回selectionRange对象(document.getSelection方法与Google Chrome、Safari和Internet Explorer中的window.getSelection方法相同)。因此,不应使用此方法。

与Firefox和Opera相比,Google Chrome、Safari和Internet Explorer中的
文档/窗口。getSelection
方法的工作方式有所不同。它在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”;如果是字符串,'window.getSelection()'with'.isCollapsed'将显示'undefined'…@trewius,因此您必须根据正在使用的浏览器执行一个条件。但无论如何,我不建议使用此方法。@未命名您能推荐其他解决方案替换window.getSelection吗?谢谢。在我的电脑中,document/window.getSelection方法也会在firefox中返回一个对象(anchorNode、anchorOffset等):“alert(window.getSelection().isCollapsed)”表示“false”;如果是字符串,'window.getSelection()'with'.isCollapsed'将显示'undefined'…@trewius,因此您必须根据正在使用的浏览器执行一个条件。但无论如何,我不建议使用此方法。@未命名您能推荐其他解决方案替换window.getSelection吗?谢谢
<div contenteditable="true"> ... text.... <img src="image.png" height="50" width="50" onmouseup="alert(window.getSelection().isCollapsed)"> ... text... </div>
<div contenteditable="true"> ... text.... <img src="image.png" alt="hello" height="50" width="50" onmouseup="alert(window.getSelection().toString())"> ... text... </div>