Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# document.selection.createRange()在chrome和safari中不起作用_C#_Javascript_Asp.net - Fatal编程技术网

C# document.selection.createRange()在chrome和safari中不起作用

C# document.selection.createRange()在chrome和safari中不起作用,c#,javascript,asp.net,C#,Javascript,Asp.net,我使用一个文本框,文本模式作为多行选项,它在IE中工作正常,Mozilla问题在Chrome和safari中出现。示例代码folows <asp:TextBox ID="txtRootDescription" onpaste="DoPaste(this);" Width="600px" Rows="10" Columns="72" MaxLength="500" TextMode="MultiLine" runat="server"></asp:TextBox> fun

我使用一个文本框,文本模式作为多行选项,它在IE中工作正常,Mozilla问题在Chrome和safari中出现。示例代码folows

<asp:TextBox ID="txtRootDescription" onpaste="DoPaste(this);" Width="600px" Rows="10" Columns="72" MaxLength="500" TextMode="MultiLine" runat="server"></asp:TextBox>

function DoPaste(control) {
maxLength = control.attributes["maxlength"].value;   
if (maxLength) {        
    var oTR = control.document.selection.createRange();     
}}

在chrome中,它给了我一个错误,因为无法读取未定义的属性“selection”

,我会使用JQuery来做同样的事情,因为它是跨浏览器的,您编写的java脚本更抽象,而不是您目前编写的本机特定于浏览器的脚本。

在不包括IE9浏览器的非IE浏览器中,请参见注释,使用window.getSelection获取选择对象。在IE<9中,原始代码应该可以工作

function GetSelection () {
    if (window.getSelection) {  // all browsers, except IE before version 9
        var selectionRange = window.getSelection ();                                        
        return selectionRange.toString();
    } 
    else {
        if (document.selection.type == 'None') {
            return "";
        }
        else {
            var textRange = document.selection.createRange ();
            return textRange.text;
        }
    }
}

function DoPaste(control) {
    maxLength = control.attributes["maxlength"].value;   
    if (maxLength) {        
        var oTR = GetSelection();     
    }
}
一般来说,使用选择和范围是非常棘手的,因为浏览器支持变化很大


这里有一个很好的参考资料,列出了浏览器支持、在哪里工作的代码以及在相应浏览器中工作的示例代码:

在获取文档中的选定文本时,有许多缺点,主要与是否在表单控件中选择文本或作为其他元素的文本有关。尝试执行以下操作的函数:

function checkForSelectedText(e) {
  var e = e || window.event;
  var el = e.target || e.srcElement;
  var tagName = el.tagName && el.tagName.toLowerCase();
  var t;
  var d = document;

  // Try DOM 2 Range - for most browsers, including IE 6+
  // However, doesn't get text selected inside form controls
  // that allow selection of text content (input type text, 
  // textarea)
  if (d && d.selection && d.selection.createRange) {
    t = d.selection.createRange().text;

  // Otherwise try HTML5 - note that getSelection returns
  // a string with extra properties. This may also get
  // text within input and textarea
  } else if (d.getSelection) {
    t = d.getSelection();
  }

  // If didn't get any text, see if event was inside
  // inupt@type=text or textarea and look for text
  if (t == '') {
    if (tagName == 'textarea' || 
       (tagName == 'input' && el.type == 'text')) {

     // Check selectionStart/End as otherwise if no text
     // selected, IE returns entire text of element
     if (typeof el.selectionStart == 'number' && 
         el.selectionStart != el.selectionEnd) {
        t = el.value.substring(el.selectionStart, el.selectionEnd)
     }
    }
  }
  return t;
}

jQuery中的哪个方法可以做到这一点?除非改进,否则这应该是一条注释。用于选择文本的最佳jquery函数:@drharris-该函数使用浏览器嗅探,因此如果您使用的浏览器不是嗅探的4,运气不好。下面的答案修改为使用空检查,而不是浏览器嗅探。选择文本与使用范围总是不同的。在IE中,您的原始代码将起作用。如果检查引用,它会告诉您在哪个浏览器中支持哪些代码。我发现它非常有用,而且他们也提供了示例cross broswer代码。Opera 10.5也支持window.getSelection。10.5前后的歌剧有什么区别?@Tim Down:你确定吗?通常dottoro在浏览器支持方面是正确的。但是无论如何,我会检查ifwindow.getSelection,而不是任何特定的浏览器版本,这样如果它在那里,代码就会工作。@Mrchief:是的,我确定。我认为让事情变得混乱的是,根据dottoro的说法,他们在10.5中删除了document.selection,但在那之前它肯定有window.getSelection,我肯定某些9.x版本有它,所以有重叠。我明白了。我编辑了我的答案以反映同样的情况。谢谢!