2

コンピューターでテキストを強調表示すると、jqueryがその選択された値を取得し、その周りにタグ、より具体的なコード、またはpreタグをスローするエディターを作成しようとしています。

    var Selectedvalue =  // set highlighted selection value
        $("#content").contents().find("body").append($("<pre></pre>").append(Selectedvalue))
   ;

タグ間の値を取得する方法はすでに知っています。値を取得する方法を知っている必要があります。

4

2 に答える 2

1

数回のグーグルからよく、http://mark.koli.ch/2009/09/use-javascript-and-jquery-to-get-user-selected-text.htmlこれがそれだと確信しています。

OPコメントへの回答

<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">

    $(function()
    {
        var ifx = $('<iframe src="code/sample.html" height=200 width=200></iframe>').appendTo(document.body);

        $(document.body).bind('mouseover', function()
        {
            var u_sel;
            if(window.getSelection)
            {
                u_sel = ifx[0].contentWindow.getSelection();
                alert(u_sel);
            }
        });
    });

</script>
于 2013-02-13T05:23:21.653 に答える
1

このようなことを試してください

function getSelectionText() {
var text = "";
if (window.getSelection) {
    text = window.getSelection().toString();
} else if (document.selection && document.selection.type != "Control") {
    text = document.selection.createRange().text;
}
return text;

}

于 2013-02-13T05:20:43.783 に答える