1

私のアプリケーションでは、マウスの太字を使用して選択したテキストを選択したいと考えています..javascriptを使用してこれを行うには? また、JavaScriptを使用してカーソル位置を知る方法...たとえば、カーソルが置かれているテキストの直前に関数を使用してテキストを挿入する必要がある場合があります

4

2 に答える 2

5

テキストエリアでこれを行うことができます:

<html>
<head>

<title>onselect test</title>

<script type="text/javascript">

window.onselect = selectText;

function selectText(e)
{
    start = e.target.selectionStart;
    end = e.target.selectionEnd;
    alert(e.target.value.substring(start, end));
}
</script>
</head>

<body>
<textarea>
Highlight some of this text
with the mouse pointer
to fire the onselect event.
</textarea>
</body>
</html>
于 2012-03-18T15:34:30.723 に答える
1

次のような意味ですか。


function getSelText()
{
    var txt = '';
     if (window.getSelection)
    {
        txt = window.getSelection();
    }
    else if (document.getSelection)
    {
        txt = document.getSelection();
    }
    else if (document.selection)
    {
        txt = document.selection.createRange().text;
    }
    else  { return; }
}
//txt is the selected text
于 2012-03-18T08:16:12.070 に答える