Bootstrap

js获取鼠标选中的文字

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div onClick="SelectText()">
        <p>游居民频染怪病</p>
        <p>11111</p>
        <p>22222</p>
        <p>33333</p>
        <p>44444</p>
    </div>

    <script>
        function SelectText() {
            // try {
            //     var selecter = window.getSelection().toString();
            //     if (selecter != null && selecter.trim() != "") {
            //         alert(selecter);
            //     }
            // } catch (err) {
            //     var selecter = document.selection.createRange();
            //     var s = selecter.text;
            //     if (s != null && s.trim() != "") {
            //         alert(s)
            //     }
            // }
            console.log(1111111, window.getSelection().toString().trim().replace(/\n/g, ""));
        }
        // //替换文本前与后的空格
        // String.prototype.trim = function () {
       //      return this.replace(/(^\s*)|(\s*$)/g, "");
        // }
    </script>
</body>

</html>
;