Select 设置网页中的最大文本选择范围

Select 设置网页中的最大文本选择范围,select,text,copy,range,max,Select,Text,Copy,Range,Max,我想用javascript在我的网页上设置文本选择的限制。 我的意思是- 如果有人试图用鼠标光标(或按ctrl+a)选择文本,则最大选择范围必须为100个字符,即他将从我的页面复制最多100个字符。有可能吗?您可以检测选择更改事件(IE和WebKit支持文档上的事件,并且您可以在其他浏览器中使用各种与按键和鼠标相关的事件)使用A,但我在做这件事之前会非常仔细地考虑:它打破了用户期望的标准选择行为,我真的无法想象一个值得去做的情况。p> 这是我的最终解决方案: <!DOCTYPE html

我想用javascript在我的网页上设置文本选择的限制。 我的意思是-
如果有人试图用鼠标光标(或按ctrl+a)选择文本,则最大选择范围必须为100个字符,即他将从我的页面复制最多100个字符。有可能吗?

您可以检测选择更改事件(IE和WebKit支持
文档上的事件,并且您可以在其他浏览器中使用各种与按键和鼠标相关的事件)使用A,但我在做这件事之前会非常仔细地考虑:它打破了用户期望的标准选择行为,我真的无法想象一个值得去做的情况。p> 这是我的最终解决方案:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <title>test</title>
  </head>
  <body>
<script language="javascript" type="text/javascript">
document.onselectionchange = zzz;

function zzz()
{
var selectedRange = returnSelection();

    if(selectedRange.length>50)
    {
    //alert('opaaaa');
    deselectSelection();
    }

}

function returnSelection()
{
    if (window.getSelection)
        {
        if (window.getSelection().toString().length > 0)
        {
        return window.getSelection().toString();
        }
    }
    else if (document.getSelection)
    {
        if (document.getSelection().toString().length > 0)
        {   
        return document.getSelection().toString();
        }
    }
        else if (document.selection)
    {
        if (document.selection.createRange().text.toString().length > 0)
        {       
        return document.selection.createRange().text.toString();
        }
    }
    return false;
}
function deselectSelection()
{       
    if (window.getSelection) 
    {
        if (window.getSelection().empty) // Chrome
        {  
        window.getSelection().empty();
        } 
        else if (window.getSelection().removeAllRanges) // Firefox
        { 
        window.getSelection().removeAllRanges();
        }
    } 
        else if (document.selection) // IE?
    {  
    document.selection.empty();
    }
}
</script>

  <p>
  A web page could be considered as a document tree that can contain any number of     branches. There are rules as to what items each branch can contain (and these are detailed     in each element’s reference in the “Contains” and “Contained by” sections). To understand     the concept of a document tree, it’s useful to consider a simple web page with typical     content features alongside its tree view, as shown in
  </p>
  </body>
</html>

测试
document.onselectionchange=zzz;
函数zzz()
{
var selectedRange=returnSelection();
如果(已选择范围长度>50)
{
//警报(“opaaaa”);
取消选择();
}
}
函数returnSelection()
{
if(window.getSelection)
{
如果(window.getSelection().toString().length>0)
{
返回窗口.getSelection().toString();
}
}
else if(document.getSelection)
{
if(document.getSelection().toString().length>0)
{   
返回文档.getSelection().toString();
}
}
else if(文档选择)
{
if(document.selection.createRange().text.toString().length>0)
{       
return document.selection.createRange().text.toString();
}
}
返回false;
}
函数选择()
{       
if(window.getSelection)
{
if(window.getSelection().empty)//Chrome
{  
window.getSelection().empty();
} 
else if(window.getSelection().removeAllRanges)//Firefox
{ 
getSelection().removeAllRanges();
}
} 
else if(document.selection)//IE?
{  
document.selection.empty();
}
}

网页可以被视为一个文档树,可以包含任意数量的分支。关于每个分支可以包含哪些项,有一些规则(这些规则在“包含”和“包含者”部分中每个元素的引用中都有详细说明)。要理解文档树的概念,可以考虑一个简单的Web页面,包括树视图中的典型内容特征,如