select - Knowing the text selected using mouse in javascript - Stack Overflow

Imy application i want to make the text seleected selected using mouse bold..How to do this using jav

I my application i want to make the text seleected selected using mouse bold..How to do this using javascript? Also how to know the cursor position using javascript...For example ,i may need to insert a text using my function just before the text where cursor is placed

I my application i want to make the text seleected selected using mouse bold..How to do this using javascript? Also how to know the cursor position using javascript...For example ,i may need to insert a text using my function just before the text where cursor is placed

Share Improve this question asked Mar 18, 2012 at 8:10 Jinu Joseph DanielJinu Joseph Daniel 6,31117 gold badges64 silver badges93 bronze badges 1
  • Insert into what? An online editor? – mplungjan Commented Mar 18, 2012 at 8:18
Add a ment  | 

3 Answers 3

Reset to default 5

You can do this in a 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>

Do you mean something like this:


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

Caching selected text, only available when it is editable input, but not in uneditable htm area i.e. when the text in div or span or etc. the above methods dosn't work .

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744376893a4571229.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信