JQuery实现选中文字弹出DIV

$(function () {
        $("#content").mouseup(function (e) {
            var x = 10;
            var y = 10;
            var text = "";
            if (document.selection) {
                text = document.selection.createRange().text;
            }
            else if (window.getSelection()) {
                text = window.getSelection();
            }
            if (text!= "") {
                var tip = "tip";
                var tooltip = "<div id='tooltip' class='tooltip'><a onclick=doit('"+text+"')>" + tip + "</a></div>";
                $("body").append(tooltip);
                $("#tooltip").css({
                    "top": (e.pageY + y) + "px",
                    "left": (e.pageX + x) + "px",
                    "position": "absolute"
                }).show("fast");
            }
        }).mousedown(function () {
            $("#tooltip").remove();
        });
    });
     
    function doit(text) {
        if (text != "") {
            alert(text);//此处你可以添加你想要的实现
        }
    }

你可能感兴趣的:(JQuery实现选中文字弹出DIV)