jquery JavaScript复制输入值

fykwrbwg  于 2023-11-17  发布在  jQuery
关注(0)|答案(3)|浏览(108)

你好伙计们我想复制输入值是的按钮点击但没有工作。请帮助!!

<script>
  function copyToClipboard(element) {
    var $temp = $("<input>");
    $("body").append($temp);
    $temp.val($(element).text()).select();
    document.execCommand("copy");
    $temp.remove();
  }
</script>   

<input type="text" class="form-control" name="myvalue"  id="myvalue" value="YEAH" readonly  />

<button class="btn btn-primary btn-block" onclick="copyToClipboard('#myvalue')">Copy myvalue</button>

字符串

x8diyxa7

x8diyxa71#

function copyToClipboard() {
    var textBox = document.getElementById("myvalue");
    textBox.select();
    document.execCommand("copy");
}

个字符

mwyxok5s

mwyxok5s2#

如果要将输入文本复制到剪贴板,请执行代码。

//HTML
<input type="text" class="form-control" name="myvalue"  id="myvalue" value="YEAH" readonly  />
<button class="btn btn-primary btn-block" onclick="copyToClipboard();">Copy myvalue</button>

//SCRIPT For ClipBoard Copy
 <Script>
    function copyToClipboard() {
        var textBox = document.getElementById("myvalue");
        textBox.select();
        document.execCommand("copy");
    }
</Script>


//For local storage variable copy
<Script>
    function copyToStorage() {
        var textBox = document.getElementById("myvalue");
        if(typeof(Storage) !== "undefined") {

            var textBox_value=textBox.value;
            localStorage.setItem("textBox_value", textBox_value);
         } else {
               alert("Sorry your browser does not support this script");
           }
    }
</Script>

   // Also there one to use an element like hidden to store the value for same page life.

字符串

x6h2sr28

x6h2sr283#

document.execCommand()现在已经过时了,替代方法是通过navigator.clipboard使用API API。根据MDN Web服务器(https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API):

function copyToClipboard() {
  var textBox = document.getElementById("myvalue");
  console.log(textBox.value)
  navigator.clipboard.writeText(textBox.value);
}

个字符

相关问题