开发者

Using window.getSelection to get a string

I'm trying to use window开发者_如何学JAVA.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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜