简体   繁体   English

window.getSelection()的值在回调期间更改

[英]Value of window.getSelection() changes during callback

I am trying to store the value of window.getSelection(), but this value seems to change if the user selects another selection. 我正在尝试存储window.getSelection()的值,但是如果用户选择其他选择,则此值似乎会更改。

In my code below, I have stored the initial user's selection. 在下面的代码中,我存储了初始用户的选择。 I then create my form (which takes a bit of time), and when the form is finished being created I fill in the form with the user's selection. 然后,我创建自己的表单(这会花费一些时间),当表单创建完成后,我会用用户的选择来填写表单。 Now during the time when the form is being created, the user may select some other text, and now the object stored in my selection variable has changed. 现在,在创建表单的过程中,用户可以选择其他文本,现在存储在我的选择变量中的对象已更改。 How would I prevent the value of selection from being changed? 如何防止选择的值被更改?

var selection = window.getSelection();

createForm(function() {
    fillInForm(selection);
});

EDIT - I need to keep the DOM structure of the selection as the form references the position of the selected text. 编辑-我需要保留所选内容的DOM结构,因为表单引用了所选文本的位置。

The getSelection function returns a Selection object. getSelection函数返回一个Selection对象。 There can only be one selection object per document, so the range associated with the selection must change if the user makes a new selection. 每个文档只能有一个选择对象,因此如果用户进行新选择,则必须更改与选择相关联的范围。

The user agent should allow the user to change the active document's selection. 用户代理应允许用户更改活动文档的选择。 If the user makes any modification to a selection, the user agent must create a new range with suitable start and end and associate the selection with this new range (not modify the existing range). 如果用户对选择进行了任何修改,则用户代理必须创建一个具有适当开始和结束位置的新范围,并将选择与该新范围相关联(而不修改现有范围)。

[...] [...]

If the user changes the selection or a script calls addRange(), the selection must be associated with a new range object, as required elsewhere in this specification. 如果用户更改选择或脚本调用addRange(),则选择必须与新的范围对象相关联,如本规范其他地方所要求的。

http://dvcs.w3.org/hg/editing/raw-file/tip/editing.html#selections http://dvcs.w3.org/hg/editing/raw-file/tip/editing.html#selections

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM