简体   繁体   English

IE中的选择范围

[英]selection range in IE

I've searched for days but couldn't find one solid solution to get the selection start / end in Internet explorer, I've tried this and this solution. 我搜索了好几天也没有找到一个固溶体来获得在Internet Explorer中选择开始/结束,我已经试过这个解决方案。

Both of them involve using document.selection.createRange() , the below code will work quite good but if you go to a new line (press enter one or more time) and then try to get the selection start/end you will end up before you pressed enter. 两者都涉及到使用document.selection.createRange() ,下面的代码将很好地工作,但是如果您转到新行(按Enter键一次或多次),然后尝试开始/结束选择,则最终会结束在您按Enter键之前。 From what I see createRange() won't select the latest empty line breaks, I don't want the code to get too messy so is there a different solution for this? 从我看到的createRange()不会选择最新的空换行符,我不希望代码变得太凌乱,所以对此有不同的解决方案吗?

function selecetion_range()
{
 var range = document.selection.createRange();
    var stored_range = range.duplicate();
    stored_range.moveToElementText(textarea[0]);
    stored_range.setEndPoint('EndToEnd', range );

    return {
          start: stored_range.text.replace(/\r/g,'').length - range.text.replace(/\r/g,'').length,
          end: stored_range.text.replace(/\r/g,'').length
 }
}

edit: 编辑:

To clarify the problem, lets say my cursor is at the end of this quote: 为了澄清这个问题,可以说我的光标在这句话的结尾:

"some random text “一些随机文本

<here>" <这里>”

once I use the above code the selection start would be: 一旦我使用上面的代码,选择开始将是:

"some random text<here>" “一些随机文本<这里>”

这里找到了解决方案,它是一个巨大的代码块,所以我不想在这里发布答案。

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

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