显示用户选择的文本

时间:2016-03-24 04:51:48

标签: javascript

 <script type="text/javascript">
        /* run on document load **/
        function disp() {
            //var text = document.getElementById('TextArea1').value;

            var text = document.getElementById('MainContent_TextArea1').value;
            //alert('a')

            if (text == null)
            {
                return false;
            } 
            else
            {
                var t = text;
          var t = text.substr(text.selectionStart, text.selectionEnd -text.selectionStart);
               document.getElementById('displayval').value = t;
                 alert(t)
                }
            }
  </script>
    <div>
    <input id="TextArea1" runat="server" type="text"/>
    <INPUT type="button" onclick= "disp()" visible="true" value="Show"/>
    <INPUT type="text"  id ="displayval" visible="true" />
    </div>

这里我试图显示用户选择的文本。但不幸的是在var t = text.substr(text.selectionStart,text.selectionEnd -text.selectionStart);得到错误。 它不起作用。这段代码有任何错误。请帮我纠正这个..

1 个答案:

答案 0 :(得分:1)

<script type="text/javascript">
    function disp() {
        var txtArea = document.getElementById('TextArea1');
        var start = txtArea.selectionStart;
        var finish = txtArea.selectionEnd;
        var sel = txtArea.value.substring(start, finish);
        document.getElementById('displayval').value = sel;
        alert(sel);
    }
</script>
<div>
    <input id="TextArea1" runat="server" type="text"/>
    <input type="button" onclick= "disp()" visible="true" value="Show"/>
    <input type="text"  id ="displayval" visible="true" />
</div>