有没有办法在 Chrome 和/或 IE 中使用 JS 选择多个文本区域?
Firefox 3 可以使用 JS 选择多个文本区域。 有没有办法在 Chrome 和 IE 中做到这一点?
我真的试图找到一种方法来选择多个 Chrome 和 IE9 中网页中的文本区域。
信息位于: http://help.dottoro.com/ljxsqnoi.php
示例位于: http://jsfiddle.net/t3sWz/
仅限 FireFox3.5+ 代码...(但这是问题):
<html>
<head>
<meta charset="UTF-8">
<style>
#trigger { background: yellow }
</style>
</head>
<body>
<p id="test">
This is some text you can highlight by dragging or clicking below.
</p>
<span id="trigger">
Click here to select "This" and "some"
</span>
<a> - Firefox only :-(</a>
<br>
<br>
<br>
<br>
<br>
<input type="button" value="Get selection" onmousedown="getSelText()">
<form name=aform>
<textarea name="selectedtext" rows="5" cols="20">
</textarea>
</form>
<script>
var testCase = function() {
var userSelection;
if (window.getSelection) {
userSelection = window.getSelection();
} // No support for IE
var textNode = document.getElementById('test').firstChild;
var theRange = document.createRange();
// select 0th–4th character
theRange.setStart(textNode, 0);
theRange.setEnd(textNode, 4);
// set user selection
userSelection.addRange(theRange);
var textNode = document.getElementById('test').firstChild;
var theRange = document.createRange();
// select 8th–12th character
theRange.setStart(textNode, 8);
theRange.setEnd(textNode, 12);
// set user selection
userSelection.addRange(theRange);
};
window.onload = function() {
var el = document.getElementById('trigger');
el.onclick = testCase;
};
function getSelText() {
var txt = '';
if (window.getSelection) {
txt = window.getSelection();
} else if (document.getSelection) {
txt = document.getSelection();
} else if (document.selection) {
txt = document.selection.createRange().text;
} else
return;
document.aform.selectedtext.value = txt;
}
</script>
</body>
Firefox 3 can select MULTIPLE areas of text with JS.
Is there a way doing this in Chrome and IE?
I really tried to find a way to select of multiple
textareas in a web page in Chrome and IE9.
Infos at:
http://help.dottoro.com/ljxsqnoi.php
Example at:
http://jsfiddle.net/t3sWz/
Code FireFox3.5+ only... (but this is the question):
<html>
<head>
<meta charset="UTF-8">
<style>
#trigger { background: yellow }
</style>
</head>
<body>
<p id="test">
This is some text you can highlight by dragging or clicking below.
</p>
<span id="trigger">
Click here to select "This" and "some"
</span>
<a> - Firefox only :-(</a>
<br>
<br>
<br>
<br>
<br>
<input type="button" value="Get selection" onmousedown="getSelText()">
<form name=aform>
<textarea name="selectedtext" rows="5" cols="20">
</textarea>
</form>
<script>
var testCase = function() {
var userSelection;
if (window.getSelection) {
userSelection = window.getSelection();
} // No support for IE
var textNode = document.getElementById('test').firstChild;
var theRange = document.createRange();
// select 0th–4th character
theRange.setStart(textNode, 0);
theRange.setEnd(textNode, 4);
// set user selection
userSelection.addRange(theRange);
var textNode = document.getElementById('test').firstChild;
var theRange = document.createRange();
// select 8th–12th character
theRange.setStart(textNode, 8);
theRange.setEnd(textNode, 12);
// set user selection
userSelection.addRange(theRange);
};
window.onload = function() {
var el = document.getElementById('trigger');
el.onclick = testCase;
};
function getSelText() {
var txt = '';
if (window.getSelection) {
txt = window.getSelection();
} else if (document.getSelection) {
txt = document.getSelection();
} else if (document.selection) {
txt = document.selection.createRange().text;
} else
return;
document.aform.selectedtext.value = txt;
}
</script>
</body>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不可以。在主要浏览器中,只有 Firefox 支持用户选择中的多个范围。其他浏览器(WebKit、Opera、IE9)确实有
Selection
API 来支持多个范围,但目前不支持。 WebKit 显然不打算很快添加支持。至于Opera和IE,我不知道。No. Of the major browsers, only Firefox supports multiple ranges within the user selection. Other browsers (WebKit, Opera, IE9) do have the
Selection
API to support multiple ranges but do not currently support it. WebKit is apparently not planning to add support any time soon. As to Opera and IE, I have no idea.