简体   繁体   中英

How to move caret position to end of current word?

I'm using a webbrowser control. How can I move the insert position for an execCommand to the end of the word, that is currently selected?

Example:

| <- current caret position

Som|eword -> move -> Someword| -> execCommand executes after current word

What I want to do is insert a line without braking the word. What happens now is:

Somew|ord -> line

Somew


ord

What should happen is:

Somew|ord -> line


Someword

This is so hacky that I'm almost embarrassed to post it, but ... you can accomplish "Inserting a line without breaking the word" "using a webbrowser control" by doing something like

webBrowser1.Url = 
            new Uri("javascript:" +
                "var tr=document.selection.createRange();" +
                "tr.expand('word');" +
                "tr.collapse(false);" +
                // "tr.select();" // Necessary to actually move the caret
                "tr.pasteHTML('<hr>');");

After the webbrowser has loaded the document that you want to manipulate, and the user has selected the text that they'd like to insert a line after. If you really need the caret moved as well, you'd need a tr.select() after the tr.collapse() .

It doesn't use the execCommand though, so it may not be suitable for your purposes. Maybe someone else can figure out a way to make this a little cleaner...

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