[英]How to get string from 'window.getSelection' and how to fix opening new tab by using 'window.open'?
[英]Using window.getSelection to get a string
我正在嘗試使用window.getSelection
來獲取一個字符串,但它正在返回一個對象。
var text = '';
text = document.getSelection();
alert(typeof(text)); //object
.getSelection()
返回一個DOMSelection對象。 DOMSelection類包含一個.toString()
方法,可將其轉換為字符串。
所以
var str = window.getSelection().toString();
alert(typeof(str)); // string.
getSelection
返回一個Selection
對象。 您可以通過調用其toString
方法來獲取所選文本。
text = document.getSelection()+'';
alert(typeof(text)); // << it returns string
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.