gpt4 book ai didi

javascript - 知道在javascript中使用鼠标选择的文本

转载 作者:行者123 更新时间:2023-11-30 09:00:22 25 4
gpt4 key购买 nike

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

最佳答案

您可以在文本区域中执行此操作:

<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>

关于javascript - 知道在javascript中使用鼠标选择的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9756941/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com