0
「スパン」をクリックすると、テキストエリア内で選択したテキストを取得したいと考えています。ボタンをクリックすると、選択範囲は有効になりますが、スパンをクリックしても機能しません。
スパンがクリックされたときに選択が失われるかもしれないが、ボタンをクリックしても起こっていないのだろうか? 修正方法?IEでスパンをクリックしたときにテキストエリアから選択したテキストを取得する
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
mousedownは私が望むふるまいではありません...私は他のボタンのようにマウスアップしてもらいたいです。 – urker
OK。ボタンはちょっと違った働きをするので、選択には影響しないと思う。とにかく、私の答えを更新... –