0
單擊「span」時,我想要在文本區域中選擇文本。當我點擊按鈕選擇的作品,但不是當我點擊跨度。
也許是因爲選擇在點擊span時丟失了,但是點擊按鈕時沒有發生? 如何解決它?只有當在IE中單擊跨度時從textarea中檢索選定的文本
function Copy() {
var theSelection = document.selection.createRange();
alert(theSelection.text);
}
<div>
<span class="Icon" onclick="Copy();"></span> <input type="button" value="Copy" onclick="Copy();" style="float:left;" />
</div>
<div style="clear:both;">
<textarea rows="2" cols="20" style="height:370px;width:800px;"></textarea>
</div>
IE!
更新:
這是我如何做到這一點,在Firefox:
if (window.getSelection){ // Firefox, Opera, Safari
var textbox = document.getElementById("box");
textbox.focus();
theSelection = document.activeElement.value.substring(document.activeElement.selectionStart, document.activeElement.selectionEnd);
alert(theSelection);
}
但它爲什麼與按鈕一起工作? – urker 2010-11-24 17:00:46