JS获取选中文本


<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <title></title>

</head>

<body>

<div onmouseup="funGetSelectTxt()">this is test 这是测试</div>

<textarea onmouseup="funGetSelectTxt()">


</textarea>


</body>

<script>

    //选中文字

    var funGetSelectTxt = function() {

        var txt = '';

        if(document.selection) {

            txt = document.selection.createRange().text;

            alert(txt+'---------');

        } else {

            txt = document.getSelection();

            alert(txt+'==========');

        }

        return txt.toString();

    };

</script>


</html>

你可能感兴趣的:(JS获取选中文本)