文本选择不起作用
text selection is not working
我想实现这一点,当我在页面上选择一些文本,然后点击一个按钮,我可以通过javascript获得这些文本。
function getSelText()
{
var t = '';
if(window.getSelection){
t = window.getSelection();
}else if(document.getSelection){
t = document.getSelection();
}else if(document.selection){
t = document.selection.createRange().text;
}
return t;
}
我有这个功能,但它不起作用。
self.Copy = function () {
alert(getSelText());
}
复制功能正在工作,但警报的结果始终为空。
此代码适用于我:
<html>
<head>
<script type="text/javascript">
function TestSelection () {
if (window.getSelection) {
var selectionRange = window.getSelection ();
alert ("The text content of the selection:'n" + selectionRange.toString ());
}
else {
if (document.selection.type == 'None') {
alert ("No content is selected, or the selected content is not available!");
}
else {
var textRange = document.selection.createRange ();
alert ("The text content of the selection:'n" + textRange.text);
}
}
}
</script>
</head>
<body>
Select some text or <button>element</button>, or do not select anything, before you click on the button below.
<br /><br />
<button onclick="TestSelection ();">Test the selection!</button>
</body>
</html>
相关文章:
- 为什么 .focus() 不起作用,而 .css(“color”,“red”) 在同一个选择器上起作用
- 角度ng变化或ng点击选择can'不起作用
- <选择>标签不起作用
- 初始化ng模型时,Angular ui选择占位符不起作用
- 动态填充Bootstrap选择选择器:change event dos'不起作用
- Highcharts:树映射选择状态不起作用
- 单击选项卡时jquery选项卡选择不起作用
- jQuery日期选择器在Codeigniter和Bootsrap模式表单中不起作用
- JQuery日期选择器在IE 7上不起作用
- jQuery选择ajax调用中附加的元素不起作用
- 引导程序选择在jquery pep中不起作用
- 当我动态创建元素时,选择的插件不起作用
- 指令中选择输入的双向绑定不起作用
- 棱角分明的日期选择器;在ngDialog中不起作用
- Jquery悬停选择不起作用
- 选择选项淘汰赛事件不起作用
- ng-单击在IE中不起作用的选项选择Angularjs
- jQuery选择器在脚本中不起作用,但在控制台中工作
- 循环遍历类名索引不起作用..选择
- .trigger(' selected:updated')不起作用.选择v1.6.1