简体   繁体   English

javascript FreeTextBox获取插入符位置(IE)

[英]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.. 嗨,我试图在freetextbox中获得插入符的位置,这只需要在IE中工作,我不知道该怎么做,它尝试了一些我在google上找到的脚本,但没有一个起作用,甚至在尝试修复它们后也做了没有给出正确的结果。

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! 通过使用WTK的答案,我知道了!

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?? 编辑:这很好,直到我单击文档内的img标记,然后看来o.CreateRange(sel)返回了[Object,ControlRangeCollection]而不是[Object,TextRange] ..有什么办法可以解决这个问题?

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.. Edit2:看来这给了我文本中的索引,而不是它后面的html中的索引,所以它毕竟不起作用。

I don't know what you're trying to achive there, but (looking at the FreeTextBox source code) they're using simply: 我不知道您要达到的目标,但是(看着FreeTextBox源代码)他们只是在使用:

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

Where this is a FTB_FreeTextBox object. 是FTB_FreeTextBox对象。 I'll try to come up with a working example, once I get a hold of IE (somewhere around this afternoon). 一旦掌握了IE(今天下午左右),我将尝试提出一个工作示例。

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

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