我試圖用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