【问题标题】:set the selected text visible in IE设置所选文本在 IE 中可见
【发布时间】:2014-12-30 19:04:40
【问题描述】:
1 Programmatically inserting text into a text box
2 Setting the caret to the end.
3 Making the caret visible (i.e. scroll the text box content)
4 Select some of the text from last programmatically,
5 **set the selected text visible.** (i.e. scroll the text box content)

我可以做 1,2,3,4。但是我做不到 5.这个问题只存在于IE9+

有什么办法吗??

【问题讨论】:

  • 提供你写的代码?

标签: javascript jquery html internet-explorer-11


【解决方案1】:

setSelectionRange() 不完全支持 IE。

所以在谷歌搜索了很多之后,我用下面的代码替换了它,它对我有用。

function setSelection(field, start, charsTobeSelected) {
    end = start + charsTobeSelected;
    if (field.createTextRange) {
        var selRange = field.createTextRange();
        selRange.collapse(true);
        selRange.moveStart('character', start);
        selRange.moveEnd('character', end);
        selRange.select();
        field.focus();
    } else if (field.setSelectionRange) {
        field.focus();
        field.setSelectionRange(start, end);
    } else if (typeof field.selectionStart != 'undefined') {
        field.selectionStart = start;
        field.selectionEnd = end;
        field.focus();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-02
    • 1970-01-01
    • 1970-01-01
    • 2011-03-06
    • 2016-02-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多