JS如何获取input中被选取的字符?

JS如何获取input中被选取的字符?

  • star65225692 - 1年前

    IE下:

    <script>
    function setValue(obj){
        var rng = document.selection.createRange();   
        document.getElementById("txt1").value=rng.htmlText; 
    }
    </script>
    <input type="text" value="set" nselect="setValue(this);">
    <input type="text" id="txt1">

    FireFox下:

    function setValue(obj){
        var start = obj.selectionStart;
        var end = obj.selectionEnd;
        document.getElementById("txt1").value=obj.value.substring(start,ed);
    }