【Javascript】猎取选中的笔墨

空话不说,直接上代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script>
        document.onmouseup = document.ondbclick= selceText;
        function selceText(){
            var txt;
            if(document.selection){
                txt = document.selection.createRange().text
            }else{
                txt = window.getSelection()+'';
            }
            if(txt){
                alert(txt);
            }
        }
    </script>
</head>
<body>

<p>哈哈哈呵呵呵呵嘿嘿</p>

</body>
</html>

页面中的笔墨,经由过程选中后自动弹出挑选的笔墨!

    原文作者:田超
    原文地址: https://segmentfault.com/a/1190000009298912
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞