I'm trying to use window.getSelection
to get a string but it's returning an object.
var text = '';
text = document.getSelection();
alert(typeof(text)); //object
.getSelection()
returns a DOMSelection object. The DOMSelection class contains a .toString()
method to turn it into a string.
So
var str = window.getSelection().toString();
alert(typeof(str)); // string.
getSelection
returns a Selection
object. You can get the selected text by calling its toString
method.
text = document.getSelection()+'';
alert(typeof(text)); // << it returns string
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.