javascript对象之 selectionStart selectionEnd

<script>
    function inserttag(){
        var text=document.getElementById('con');
        text.focus();
        var start=text.selectionStart;
        var end = text.selectionEnd;
        var ss = text.value.substring(start, end);
        alert(ss);
        //alert(start);
    }
</script>

<form action="" method="post">
    <textarea name="text" id="con" cols="30"  rows="10">The text selection appears here</textarea>
    <input type="submit" onclick="inserttag()" name="sub" value="OK">
</form>

用鼠标选取一块 效果图:

<html>  
<head>  
<title>Selection Start and End example</title>  
    <script type="text/javascript">  
        function SelectSomeText () {  
            var input = document.getElementById ("Textbox");  
                input.selectionStart = 4;  
                input.selectionEnd = 13;                  
        }         
        
    </script>  
</head>  
<body>  
    <p><input type="text" id="Textbox" size="40" value="The text selection appears here"/></p>  
    <p><button onclick="SelectSomeText ()">See selection</button></p>      
</body>  
</html>        

效果图:

原文地址:https://www.cnblogs.com/perseverancevictory/p/3665814.html