2011-03-24 54 views
22
不工作

有人可以幫助我得到這個代碼在IE工作,請:getSelection()在IE

HTML:

<p>Alex Thomas</p> 
<button id="click">Click</button> 

JS

$('#click').click(function(){ 
    var range = window.getSelection().getRangeAt(0); 
    var selectionContents = range.extractContents(); 
    var span = document.createElement("span"); 
    span.style.color = "red"; 
    span.appendChild(selectionContents); 
    range.insertNode(span); 
}); 

編寫了這裏:http://jsfiddle.net/WdrC2/

在此先感謝...

+1

@ 9之前的亞馬遜IE不執行'getSelection()'。 – 2011-03-24 15:52:56

+0

哪個版本的IE? – Ryre 2011-03-24 15:53:12

+0

該代碼適用於Chrome 8中的我。我選擇了文本並單擊了該按鈕,文本變爲紅色,所以有些作品可行。 – Blender 2011-03-24 15:55:47

回答

22

IE 9之前不支持window.getSelection()。您可以改爲使用document.selection(有關說明,請參見this msdn page)。此選擇對象的方法createRange()返回TextRange對象(有關詳細信息,請參閱this msdn page)。

請注意,selectiontextrange對象都是微軟自己的實現,並且不遵循W3C標準。您可以閱讀關於www.quirksmode.org/dom/range_intro.html上的textrangerange問題的更多信息。

下面的實現在IE:

$('#click').click(function(){ 
    var range = document.selection.createRange(); 
    range.pasteHTML("<span style='color: red'>" + range.htmlText + "</span>"); 
}); 

這不是幾乎一樣好其他的實現,因爲你有一個字符串,而不是DOM工作。有一點點,你粘貼<span id="myUniqueId"></span>作爲佔位符,然後用dom替換它。儘管如此,您仍需要使用range.htmlTextrange.text

順便說一句:上面的實現顯然是IE只有。您必須使用一些瀏覽器功能檢測來決定使用哪個版本。

+0

很好的答案,我會試試這個,謝謝。 – Alex 2011-03-25 09:58:02

+0

你確定IE9不支持'window.getSelection()'? http://msdn.microsoft.com/en-us/library/ie/ff975169(v=vs.85).aspx說它.. – Rijk 2013-05-10 09:55:11

+0

@瑞克你是對的,這就是爲什麼我寫了「IE *之前*到9不支持window.getSelection()。「 :) – 2013-05-10 14:08:51

-3

如果你想要的顏色 「亞歷克斯·托馬斯」 爲紅色,你可以做

HTML

<p id='txt'>Alex Thomas</p> 
<input type='button' id='btn' value='click' /> 

JS

$('#click').click(function(){ 
    $('#txt').attr('color','Red'); 
}); 
+0

一個毫無意義的答案,但無論如何感謝。 – Alex 2011-03-24 16:20:08

+4

他想讓'Alex Thomas'的突出部分變紅,而不是整個事情。 – 2011-03-24 18:40:55

1

測試這一位置:http://jsfiddle.net/6BrWe/

這是一個黑客位的和不那麼漂亮,但應以IE和其他瀏覽器 - 我沒有做過很多跨瀏覽器測試,雖然:)

$('#click').click(function() { 
    var whatBrowser = getIt(); 
    if (whatIsIt == 'notIE' && whatBrowser) { 
     notIE(whatBrowser); 
    } 
    else if (whatIsIt == "isIE"&& whatBrowser) { 
     isIE(whatBrowser); 
    }; 
}); 

var whatIsIt = ""; 

function getIt() { 
    if (window.getSelection) { 
     whatIsIt = "notIE"; 
     return window.getSelection(); 
    } 
    else if (document.getSelection) { 
     whatIsIt = "notIE"; 
     return document.getSelection(); 
    } 
    else { 
     var selection = document.selection && document.selection.createRange(); 
     if (selection.text) { 
      whatIsIt = "isIE"; 
      return selection; 
     }; 
     return false; 
    }; 
    return false; 
}; 

function isIE(selection) { 
    if (selection) { 
     var selectionContents = selection.text; 
     if (selectionContents) { 
      selection.pasteHTML('<span class="reddy">' + selectionContents + '</span>'); 
     }; 
    }; 
}; 

function notIE(selection) { 
    var range = window.getSelection().getRangeAt(0); 
    var selectionContents = range.extractContents(); 
    var span = document.createElement("span"); 
    span.className= "reddy"; 
    span.appendChild(selectionContents); 
    range.insertNode(span); 
}; 
+0

隨着所有的JS,我不得不想知道爲什麼你不只爲第一行做一個'document.getElementById(「click」)。onclick'。但是,對於使用功能檢測的完整答案+1。 – 2011-03-24 21:49:43

+0

OP在那裏有jQuery,所以我沒有改變那個部分 – 2011-03-24 22:14:58

0

對於一個跨瀏覽器的解決方案看這個問題在計算器上:Is there a cross-browser solution for getSelection()?

這個問題是不是這一個相當重複的,所以我覺得這個問題在這個答案鏈接,而不是它是有用的。我做了這個答案社區維基。