简体   繁体   中英

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.

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.

The getSelection function returns a Selection object. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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