こんにちは、このテキストのコピー機能を正常に機能させる方法はありますか?まだ解決策がない選択の前に矢印関数を配置しました。ありがとうございました。
function paste() {
let toSelect = ('Select text!!!!!!!!!!!!!!!! ');
console.log(toSelect);
toSelect.select();
toSelect.setSelectionRange(0, 99999);
document.execCommand('copy');
alert("Copied the text: " + toSelect);
}
<button onclick=paste(); type="button">Click Me!</button>
私が見る最初のエラーHTMLInputElement.select()
は、許可されていない文字列でメソッドを使用しようとしていることです。変数toSelect
は<input>
or<textarea>
要素ではないため、select()
メソッドは呼び出されません。
このselect()
メソッドは、<textarea>
要素または<input>
テキストフィールドを含む要素内のすべてのテキストを選択します。HTMLInputElement.select()-MDN
function paste() {
const text = document.getElementById("text");
const output = document.getElementById("paste-output");
text.select();
text.setSelectionRange(0, 99999);
document.execCommand('copy');
alert("Copied the text: " + text.value);
output.textContent = text.value;
}
<textarea id="text">Some text to copied</textarea>
<button onclick=paste(); type="button">Click Me!</button>
<span id="paste-output"></span>
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加