在Chrome和/或IE中用JS来选择多个文本区域吗? [英] Is there a way selecting MULTIPLE areas of text with JS in Chrome and/or IE?

查看:175
本文介绍了在Chrome和/或IE中用JS来选择多个文本区域吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Firefox 3可以使用JS选择多个文本区域。
在Chrome和IE中是否有这样做的方法?



我真的试图找到一种方法来在网页中选择多个
textareas在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>
这是您可以通过拖动或点击下面突出显示的文本。
< / p>
< span id =trigger>
点击此处选择This和some
< / span>
< a> - 仅适用于Firefox: - (< / a>
< br>
< br>
< br>
< br>
< br> ;
< input type =buttonvalue =Get selectiononmousedown =getSelText()>
< form name = aform>
< textarea name =selectedtext rows =5cols =20>
< / textarea>
< / form>
< script>
var testCase = function
var userSelection;

if(window.getSelection){
userSelection = window.getSelection();
} //不支持IE
var textNode = document.getElementById('test')。firstChild;
var theRange = document.createRange();

//选择第0到第4个字符
theRange.setStart(textNode ,0);
theRange.setEnd(textNode,4);

//设置用户选择
userSelection.addRange(theR安格);

var textNode = document.getElementById('test')。firstChild;
var theRange = document.createRange();

//选择第8到第12个字符
theRange.setStart(textNode,8);
theRange.setEnd(textNode,12);

//设置用户选择
userSelection.addRange(theRange);
};

window.onload = function(){
var el = document.getElementById('trigger');
el.onclick = testCase;
};

函数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支持用户选择中的多个范围。其他浏览器(WebKit,Opera,IE9)确实有 Selection API来支持多个范围,但目前不支持。 WebKit是显然不打算增加支持很快一>。至于Opera和IE,我不知道。


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>

解决方案

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.

这篇关于在Chrome和/或IE中用JS来选择多个文本区域吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆