亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 JavaScript 將多個標簽中特定標簽的文本復制到剪貼板

使用 JavaScript 將多個標簽中特定標簽的文本復制到剪貼板

當年話下 2023-05-25 17:21:53
我想使用 javascript 將輸入復制到我的剪貼板,所以有什么辦法可以做,謝謝無需輸入字段JavaScript 和 HTMLfunction copy(input){     }<p>Text To Copy = hi <button type="button" onclick="copy('hi')">click to copy</button></p>
查看完整描述

3 回答

?
慕姐8265434

TA貢獻1813條經驗 獲得超2個贊

您可以使用navigator.clipboard.writeText將文本復制到剪貼板。


function copy(input) {

  if (navigator.clipboard) {

    navigator.clipboard.writeText(input).then(() => {

      console.log('Copied to clipboard successfully.');

    }, (err) => {

      console.log('Failed to copy the text to clipboard.', err);

    });

  } else if (window.clipboardData) {

    window.clipboardData.setData("Text", input);

  }

}

<p>Text To Copy = hi <button type="button" onclick="copy('hi')">click to copy</button></p>


查看完整回答
反對 回復 2023-05-25
?
慕姐4208626

TA貢獻1852條經驗 獲得超7個贊

function copy_text_fun() {

    //getting text from P tag

    var copyText = document.getElementById("copy_txt");  

    // creating textarea of html

    var input = document.createElement("textarea");

    //adding p tag text to textarea 

    input.value = copyText.textContent;

    document.body.appendChild(input);

    input.select();

    document.execCommand("Copy");

    // removing textarea after copy

    input.remove();

    alert(input.value);

}

<p id="copy_txt">hi</p>

<button  onclick="copy_text_fun()">Copy</button>


查看完整回答
反對 回復 2023-05-25
?
喵喔喔

TA貢獻1735條經驗 獲得超5個贊

請試試這個。也許它會為你工作。


 function myFunction() {

      /* Get the text field */

      var copyText = document.getElementById("myInput");

    

      /* Select the text field */

      copyText.select();

      copyText.setSelectionRange(0, 99999); /*For mobile devices*/

    

      /* Copy the text inside the text field */

      document.execCommand("copy");

    

      /* Alert the copied text */

      alert("Copied the text: " + copyText.value);

    }

 <input type="text" value="Hello World" id="myInput">

    <button onclick="myFunction()">Copy text</button>


查看完整回答
反對 回復 2023-05-25
  • 3 回答
  • 0 關注
  • 186 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號