个人小站

[Javascript]处理火狐getSelection无法读取文本框及输入框里内容的问题

字数统计: 389阅读时长: 2 min
2021/08/05

attention:不仅仅是文本框内的选择会被脚本记录,在本页面内所有的选取都会被记录

Drag to select text (script runs on mouseup)



源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<script type="text/javascript">
function GetSelectedText (){
var userSelection, ta;
if (window.getSelection && document.activeElement){
if (document.activeElement.nodeName == "TEXTAREA" ||
(document.activeElement.nodeName == "INPUT" &&
document.activeElement.getAttribute("type").toLowerCase() == "text")){
ta = document.activeElement;
userSelection = ta.value.substring(ta.selectionStart, ta.selectionEnd);
} else {
userSelection = window.getSelection();
}
document.getElementById("seltext").innerHTML += "Selection="+userSelection.toString()+" [1]<br>";
} else {
// all browsers, except IE before version 9
if (document.getSelection){
userSelection = document.getSelection();
document.getElementById("seltext").innerHTML += "Selection="+userSelection.toString()+" [2]<br>";
}
// IE below version 9
else if (document.selection){
userSelection = document.selection.createRange();
document.getElementById("seltext").innerHTML += "Selection="+userSelection.text+" [3]<br>";
}
}
}
</script>
</head>
<body onmouseup="GetSelectedText()">
<h1>Drag to select text (script runs on mouseup)</h1>
<textarea rows="5" cols="30">Select text or part of it.</textarea><br>
<input type="text" name="text1" value="Some more play text" size="50"><br>
<div id="seltext"></div>

来源

http://forums.mozillazine.org/viewtopic.php?f=25&t=2268557

原文作者:ted423

原文链接:http://ted423.github.io/Code/WWW/getSelection-in-Firefox/

发表日期:August 5th 2021, 7:32:07 pm

更新日期:January 13th 2015, 9:14:00 pm

版权声明:本站原创内容(一般是语句不通顺的那种)采用知识共享署名-非商业性使用 4.0 国际许可协议进行许可,转载内容以及不带个人观点的分享不在此例,摘抄有Wiki的内容的文章统一根据Wiki采用 CC BY-SA 3.0

CATALOG
  1. 1. Drag to select text (script runs on mouseup)
  2. 2. 源码
  3. 3. 来源