我试图用window.getSelection
来得到一个字符串,但它返回一个对象。使用window.getSelection得到一个字符串
var text = '';
text = document.getSelection();
alert(typeof(text)); //object
我试图用window.getSelection
来得到一个字符串,但它返回一个对象。使用window.getSelection得到一个字符串
var text = '';
text = document.getSelection();
alert(typeof(text)); //object
.getSelection()
返回一个DOMSelection对象。 DOMSelection class包含一个.toString()
方法将它变成一个字符串。
所以
var str = window.getSelection().toString();
alert(typeof(str)); // string.
getSelection
返回Selection
对象。您可以通过调用toString
方法来获取选定的文本。
text = document.getSelection()+'';
alert(typeof(text)); // << it returns string
感谢您的回答。我在哪里可以找到完整的JavaScript“类”。 – nXqd 2010-03-10 15:43:30