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
3 Answers
Reset to default 5You 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条)