2011-08-11 95 views
0

我使用textmode作爲多行選項的文本框在IE中工作正常,Mozilla問題出現在Chrome和Safari瀏覽器中。示例代碼folowsdocument.selection.createRange()不能在鉻和safari中工作

<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();  
}} 

鉻它給了我一個錯誤「未定義無法讀取屬性‘選擇’」

回答

0

我會使用jQuery做同樣的,因爲它是跨瀏覽器,你寫出更抽象的java腳本,而不是您迄今爲止寫的本地瀏覽器特定的一個。

+1

jQuery中的哪些方法可以做到這一點?無論如何,這應該是一個評論(除非改進)。 – Mrchief

+1

選擇文本的最佳jquery函數:http://stackoverflow.com/questions/985272/jquery-selecting-text-in-an-element-akin-to-highlighting-with-your-mouse/987376#987376 – drharris

+1

@drharris - 該功能使用瀏覽器嗅探,所以如果你使用瀏覽器以外的瀏覽器進行瀏覽,運氣不好。 – RobG

5

非IE瀏覽器(不包括IE9)瀏覽器(請參閱評論),使用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();  
    } 
} 

一般來說,selectionranges工作非常棘手,因爲瀏覽器支持的變化這麼大。

這裏有一個很好的參考,其中列出了瀏覽器的支持(什麼代碼工作在哪裏),並在相應的瀏覽器上運行的代碼示例:http://help.dottoro.com/ljefwsqm.php

+0

當我給IE中的window.getSelection它顯示未定義 –

+0

在IE中,您的原始代碼將工作。如果您檢查參考,它會告訴您在哪個瀏覽器中支持哪些代碼。我覺得它非常有幫助,他們也有樣品交叉編碼。 – Mrchief

+0

Opera 10.5也支持'window.getSelection()'。 Opera 10.5之前和之後有什麼區別? –

1

有許多弱點的時候文檔中得到選定的文本,大多與是否在表單控件中選擇文本或作爲其他元素的文本。嘗試執行如下功能:

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 
    // [email protected]=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; 
} 
相關問題