admin管理员组文章数量:1401401
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: .php
Example at: /
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>
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./ljxsqnoi.php
Example at: http://jsfiddle/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>
Share
Improve this question
asked Feb 13, 2011 at 16:20
Klaus KloseKlaus Klose
711 silver badge2 bronze badges
1
- Why do you want to do this? I'm interested in the use case. – Tim Down Commented Feb 13, 2011 at 16:45
1 Answer
Reset to default 7No. 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.
本文标签: javascriptIs there a way selecting MULTIPLE areas of text with JS in Chrome andor IEStack Overflow
版权声明:本文标题:javascript - Is there a way selecting MULTIPLE areas of text with JS in Chrome andor IE? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744309099a2599944.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论