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