admin管理员组文章数量:1394181
I'm getting the selected text from textarea but I can't get it from a div. I'm trying with document.getElementById('myDiv') but it doesn't work.
To be more specific. When I have text, using this method I can get the selected text
function TestSelection ()
{
if (window.getSelection) {
var selectionRange = window.getSelection();
alert ("The text content of the selection:\n" + selectionRange.toString());
}
}
but I cant specify the div to get it's selected text. Only from this div, not from another.
I tried var value = document.getElementById("myDiv").innerHTML;
and then value.getSelection
but it doesn't work too.
Thank you very much!!!
I'm getting the selected text from textarea but I can't get it from a div. I'm trying with document.getElementById('myDiv') but it doesn't work.
To be more specific. When I have text, using this method I can get the selected text
function TestSelection ()
{
if (window.getSelection) {
var selectionRange = window.getSelection();
alert ("The text content of the selection:\n" + selectionRange.toString());
}
}
but I cant specify the div to get it's selected text. Only from this div, not from another.
I tried var value = document.getElementById("myDiv").innerHTML;
and then value.getSelection
but it doesn't work too.
Thank you very much!!!
- This might help: stackoverflow./q/5379120/1066234 – Avatar Commented Mar 20, 2023 at 14:50
5 Answers
Reset to default 4Simply ,do the following :
var ss=getSelection();
ss.baseNode.data.substring(ss.baseOffset,ss.extentOffset);
Use the innerHTML property
var html = document.getElementById('myDiv').innerHTML;
You should use innerHTML
property:
var value = document.getElementById("myDiv").innerHTML;
You should use Selection
var selObj = window.getSelection();
window.alert(selObj);
for jquery
var str = $("#myDiv").text();
str is the text
本文标签: javascriptHow to get Selected Text from DIVStack Overflow
版权声明:本文标题:javascript - How to get Selected Text from DIV - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744721667a2621731.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论