以编程方式选择输入字段中的部分文本

发布于 2024-07-15 06:12:08 字数 113 浏览 4 评论 0原文

如何以编程方式在 HTML input 字段中选择特定范围的文本? (我不想选择整个字段,而只是选择一个子集)

另外,如何确定字段中当前选择的范围?

How can I programmatically select a specific range of text in an HTML input field? (I don't want to select the entire field, just a subset)

Also, how can I determine the currently selected range in a field?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

所有深爱都是秘密 2024-07-22 06:12:08

以下是如何选择文本框的一部分(范围选择)并获取所选文本:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title> Test </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
  window.onload = function() {
    var message = document.getElementById('message');
    // Select a portion of text
    createSelection(message, 0, 5);
    // get the selected portion of text
    var selectedText = message.value.substring(message.selectionStart, message.selectionEnd);
    alert(selectedText);
  };

  function createSelection(field, start, end) {
    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();
    }
  }
</script>
</head>
<body>
<input type="text" name="message" id="message" value="Hello World" />
</body>
</html>

演示

Here's how to select a portion of a text box (range select) and get the selected text:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title> Test </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
  window.onload = function() {
    var message = document.getElementById('message');
    // Select a portion of text
    createSelection(message, 0, 5);
    // get the selected portion of text
    var selectedText = message.value.substring(message.selectionStart, message.selectionEnd);
    alert(selectedText);
  };

  function createSelection(field, start, end) {
    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();
    }
  }
</script>
</head>
<body>
<input type="text" name="message" id="message" value="Hello World" />
</body>
</html>

Demo

夏夜暖风 2024-07-22 06:12:08

小修正。 看来 IE moveEnd() 方法是增量移动的,因此 selRange.moveEnd('character', end) 应替换为 selRange.moveEnd('character', end-start) >:

function createSelection(field, start, end) {
    if( field.createTextRange ) {
        var selRange = field.createTextRange();
        selRange.collapse(true);
        selRange.moveStart('character', start);
        selRange.moveEnd('character', end-start);
        selRange.select();
    } else if( field.setSelectionRange ) {
        field.setSelectionRange(start, end);
    } else if( field.selectionStart ) {
        field.selectionStart = start;
        field.selectionEnd = end;
    }
    field.focus();
} 

Small correction. It seems that IE moveEnd() method moves incrementally so selRange.moveEnd('character', end) should be replaced with selRange.moveEnd('character', end-start):

function createSelection(field, start, end) {
    if( field.createTextRange ) {
        var selRange = field.createTextRange();
        selRange.collapse(true);
        selRange.moveStart('character', start);
        selRange.moveEnd('character', end-start);
        selRange.select();
    } else if( field.setSelectionRange ) {
        field.setSelectionRange(start, end);
    } else if( field.selectionStart ) {
        field.selectionStart = start;
        field.selectionEnd = end;
    }
    field.focus();
} 
清欢 2024-07-22 06:12:08

谢谢你! 我现在想分享我的功能,它与即时通讯工具一起在 Ajaxel CMS 中使用

,wrapText:function(o, ot, ct) {
    var s = o[0].selectionStart; 
    var e = o[0].selectionEnd;
    o.val(o.val().substring(0, s)+ot+o.val().substring(s,e)+ct+o.val().substring(e, o.val().length)); 
    if (o[0].createTextRange){
        var sr = o[0].createTextRange(); 
        sr.collapse(true); 
        sr.moveStart('character', s); 
        sr.moveEnd('character',e-s+ot.length+ct.length); 
        sr.select();
    }
    else if(o[0].setSelectionRange){ 
        o[0].setSelectionRange(s,e+ot.length+ct.length); 
    }
    else if(o[0].selectionStart){ 
        o[0].selectionStart=s; 
        o[0].selectionEnd=e+ot.length+ct.length; 
    }
}

Thank you! I wanna share my function now, it's used in Ajaxel CMS along with Instant messenger

,wrapText:function(o, ot, ct) {
    var s = o[0].selectionStart; 
    var e = o[0].selectionEnd;
    o.val(o.val().substring(0, s)+ot+o.val().substring(s,e)+ct+o.val().substring(e, o.val().length)); 
    if (o[0].createTextRange){
        var sr = o[0].createTextRange(); 
        sr.collapse(true); 
        sr.moveStart('character', s); 
        sr.moveEnd('character',e-s+ot.length+ct.length); 
        sr.select();
    }
    else if(o[0].setSelectionRange){ 
        o[0].setSelectionRange(s,e+ot.length+ct.length); 
    }
    else if(o[0].selectionStart){ 
        o[0].selectionStart=s; 
        o[0].selectionEnd=e+ot.length+ct.length; 
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文