了解在javascript中使用鼠标选择的文本

Jin*_*iel 1 javascript mouse select bold italic

我的应用程序我想使用鼠标粗体选择选择的文本..如何使用 javascript 执行此操作?还有如何使用javascript知道光标位置...例如,我可能需要在光标所在的文本之前使用我的函数插入文本

小智 5

您可以在 textarea 中执行此操作:

<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>
Run Code Online (Sandbox Code Playgroud)