简体   繁体   中英

javascript FreeTextBox get caret position (IE)

Hi im trying to get the caret position inside the freetextbox this only needs to work in IE, i have no idea how to do it tried a few scripts that i found with google but non of them worked and even after trying to fix them they did not give the right result..

So is there any way to do it? seems like some thing so easy to do but it isn't...

By using WTK's answer i figured it out i think!

function GetCaretPossion(o) {
    var r = document.selection.createRange();
    var sel = o.GetSelection();
    var re = o.CreateRange(sel);

    var rc = re.duplicate();
    re.moveToBookmark(r.getBookmark());
    rc.setEndPoint('EndToStart', re);
    return rc.text.length;
}

Edit: This worked great until i clicked a img tag inside the document then it seems o.CreateRange(sel) returns a [Object, ControlRangeCollection] instead of a [Object, TextRange].. is there any way to solve this problem??

Edit2: It seems this gives me the index in the text and not the index in the html behind it so it did not work after all..

I don't know what you're trying to achive there, but (looking at the FreeTextBox source code) they're using simply:

sel = this.GetSelection();
r = this.CreateRange(sel);

Where this is a FTB_FreeTextBox object. I'll try to come up with a working example, once I get a hold of IE (somewhere around this afternoon).

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