简体   繁体   English

通过按钮将内容从 div 元素复制到剪贴板

[英]Copy content from div element via button to clipboard

I am working on a simple program that takes input from user and performs arithmetic function on the input value and then displays result in div element via innerHTML.我正在开发一个简单的程序,它接受用户的输入并对输入值执行算术 function,然后通过 innerHTML 在 div 元素中显示结果。 I have tried the ClipboardJS library with implementing data target in button to target div element content but it's not showing any result.我已经尝试使用 ClipboardJS 库在按钮中实现数据目标以定位 div 元素内容,但它没有显示任何结果。

Can you help me, please?你能帮我吗?

 $(document).ready(function() { document.getElementById("idofdiv").style.display = "none"; document.getElementById("copybutton").style.display = "none"; $("#showbuttonid").click(function() { var a = $("input#inputvaluebox").val(); var b = a * 10; var b = "Value is" + b; document.getElementById("idofdiv").style.display = "block"; document.getElementById("copybutton").style.display = "block"; document.getElementById("idofdiv").innerHTML = b; }); });
 @import url('https://fonts.googleapis.com/css2?family=Open+Sans:wght@600&display=swap'); .labelclass { font-size: 20px; font-family: 'Open Sans', sans-serif; }.inputvaluecss { box-sizing: border-box; border: 1px solid #DDDDDD; outline: none; margin-left: 42px; margin-top: 16px; width: 275px; padding-top: 12px; padding-left: 15px; padding-bottom: 12px; }.inputvaluecss:focus { box-shadow: 0 0 5px #2EDC29; border: 1px solid #2EDC29; }.divclass { font-size: 20px; font-family: 'Open Sans', sans-serif; color: black; background-color: white; width: 700px; padding-left: 18px; padding-top: 13px; padding-bottom: 13px; padding-right: 18px; box-sizing: border-box; border: 1px solid #DDDDDD; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script> <form> <br> <label for="inputvaluebox" class="labelclass">Input Value</label> <input type="text" id="inputvaluebox" name="inputvaluename" class="inputvaluecss"><br><br> <input type="reset" id="resetbuttonid"> <button type="button" id="showbuttonid">Show</button><br><br><br> <div id="idofdiv" class="divclass"></div><br> <button type="button" id="copybutton" data-clipboard-target="#idofdiv">Copy content!</button> </form>

What you are doing is trying to add math functions to string In your case a is a string and b is a number So you try to convert a to number and b to string.您正在做的是尝试将数学函数添加到字符串在您的情况下, a是字符串, b是数字所以您尝试将a转换为数字,将b转换为字符串。 That won't work This worked for me那行不通 这对我有用

$(document).ready(function() {
  document.getElementById("idofdiv").style.display = "none";
  document.getElementById("copybutton").style.display = "none";

  $("#showbuttonid").click(function() {
    var a = $("input#inputvaluebox").val();
    var c = parseInt(a)
    var b = c * 10;
    var d = "Value is" + b;
    document.getElementById("idofdiv").style.display = "block";
    document.getElementById("copybutton").style.display = "block";
    document.getElementById("idofdiv").innerHTML = d;
  });
});

You just need one more line of code.你只需要多一行代码。 Just take a read in the official documentation: https://clipboardjs.com/#setup只需阅读官方文档: https://clipboardjs.com/#setup

  new ClipboardJS('#copybutton')

 $(document).ready(function() { document.getElementById("idofdiv").style.display = "none"; document.getElementById("copybutton").style.display = "none"; $("#showbuttonid").click(function() { var a = $("input#inputvaluebox").val(); var b = a * 10; var b = "Value is " + b; document.getElementById("idofdiv").style.display = "block"; document.getElementById("copybutton").style.display = "block"; document.getElementById("idofdiv").innerHTML = b; }); new ClipboardJS('#copybutton') });
 @import url('https://fonts.googleapis.com/css2?family=Open+Sans:wght@600&display=swap'); .labelclass { font-size: 20px; font-family: 'Open Sans', sans-serif; }.inputvaluecss { box-sizing: border-box; border: 1px solid #DDDDDD; outline: none; margin-left: 42px; margin-top: 16px; width: 275px; padding-top: 12px; padding-left: 15px; padding-bottom: 12px; }.inputvaluecss:focus { box-shadow: 0 0 5px #2EDC29; border: 1px solid #2EDC29; }.divclass { font-size: 20px; font-family: 'Open Sans', sans-serif; color: black; background-color: white; width: 700px; padding-left: 18px; padding-top: 13px; padding-bottom: 13px; padding-right: 18px; box-sizing: border-box; border: 1px solid #DDDDDD; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script> <form> <br> <label for="inputvaluebox" class="labelclass">Input Value</label> <input type="text" id="inputvaluebox" name="inputvaluename" class="inputvaluecss"><br><br> <input type="reset" id="resetbuttonid"> <button type="button" id="showbuttonid">Show</button><br><br><br> <div id="idofdiv" class="divclass"></div><br> <button type="button" id="copybutton" data-clipboard-target="#idofdiv">Copy content!</button> </form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM