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

了解在javascript中使用鼠标选择的文本,javascript,select,mouse,bold,italic,Javascript,Select,Mouse,Bold,Italic,我想在我的应用程序中使用鼠标粗体选择文本。如何使用javascript实现这一点? 另外,如何使用javascript知道光标的位置……例如,我可能需要在光标所在的文本之前插入一个使用我的函数的文本 function getSelText() { var txt = ''; if (window.getSelection) { txt = window.getSelection(); } else if (document.getSel

我想在我的应用程序中使用鼠标粗体选择文本。如何使用javascript实现这一点?
另外,如何使用javascript知道光标的位置……例如,我可能需要在光标所在的文本之前插入一个使用我的函数的文本


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

你的意思是这样的:


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

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

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

onselect测试
window.onselect=选择文本;
功能选择文本(e)
{
开始=e.target.selectionStart;
结束=e.target.selectionEnd;
警报(如目标值子字符串(开始、结束));
}
突出显示本文中的一些内容
用鼠标指针
启动onselect事件。

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

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

onselect测试
window.onselect=选择文本;
功能选择文本(e)
{
开始=e.target.selectionStart;
结束=e.target.selectionEnd;
警报(如目标值子字符串(开始、结束));
}
突出显示本文中的一些内容
用鼠标指针
启动onselect事件。

插入什么?在线编辑?插入什么?一个在线编辑?为什么所有这些都是为了?支持哪些浏览器?我会检查…当选择发生时,是否有任何事件处理程序…我希望有类似于…onselect等的东西…为什么所有这些都是if-else for?支持哪些浏览器?我会检查…当选择发生时,是否有任何事件处理程序…我希望有…onselect等。。