簡體   English   中英

將文本從 textarea 復制到剪貼板 - Python

[英]Copy text from textarea to clipboard - Python

我正在嘗試啟用一個復制按鈕,該按鈕將從文本區域復制內容,我給出了我的 HTML 文件和 JS 的示例,我嘗試了所有方法,但沒有成功。 謝謝你的幫助。

我的 HTML

    {% if trans != "" %}
    <br>
        <div id="sTransContainer">
            <h1>Trans</h1>
            <textarea style="resize:none" cols="5" rows="10" id="sText">{{ trans }}</textarea>
            <div class="right btn-group">
                <button onclick="myFunction()">Copy text</button>
                <script async src="js/copy.js"></script>
            </div>
        </div>
    {% endif %}

我的 copy.js

function myFunction() {
  var copyText = document.getElementById("trans");
  copyText.select();
  copyText.setSelectionRange(0, 99999)
  document.execCommand("copy");
  alert("Copied the text: " + copyText.value);
}

只需用 sText 切換 trans

 function myFunction() { var copyText = document.getElementById("sText"); copyText.select(); copyText.setSelectionRange(0, 99999) document.execCommand("copy"); alert("Copied the text: " + copyText.value); }
 <div id="sTransContainer"> <h1>Trans</h1> <textarea style="resize:none" cols="5" rows="10" id="sText">{{ trans }}</textarea> <div class="right btn-group"> <button onclick="myFunction()">Copy text</button> <script async src="js/copy.js"></script> </div> </div>

您也可以使用navigator.clipboard https://developer.mozilla.org/en-US/docs/Web/API/Navigator/clipboard

 function copyToClipboard() { let clip = navigator.clipboard; if (clip === undefined) { console.log( "Upgrade your browser to use the clipboard feature.", ); } else { navigator.clipboard.writeText(document.getElementById('my_input').value); } }
 <input id='my_input' /> <button onClick='copyToClipboard()' > Click me </button>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM