如何在javascript中复制变量的值 [英] How to copy the value of variable in javascript
本文介绍了如何在javascript中复制变量的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将字符串变量复制到剪贴板。
I'm trying to copy string variable to clipboard.
我可以找到很多示例,这些示例从<中复制值。输入>或< p>标签,但是如果我对变量进行了更改,那么我的问题是我想复制它,例如本例
I could find a lot of examples that copy the values from < input > or < p > tags, But my problem in case I make a changes on variable then I want to copy it such as this example
var num1= getNum1();
var num2= getNum2();
var result = ((num1*num2)/3)-1
...//copy result value
alert("your results has been copied");
推荐答案
var getNum1 = function() {
return 1;
};
var getNum2 = function() {
return 2;
};
var num1 = getNum1();
var num2 = getNum2();
var result = ((num1 * num2) / 3) - 1;
var executeCopy = function() {
var copyhelper = document.createElement("input");
copyhelper.className = 'copyhelper'
document.body.appendChild(copyhelper);
copyhelper.value = result;
copyhelper.select();
document.execCommand("copy");
document.body.removeChild(copyhelper);
};
document.getElementById('mybutton').addEventListener('click', function() {
executeCopy();
alert("your results has been copied");
});
.copyhelper {
position: absolute;
}
<button id='mybutton'>Copy to clipboard</button>
希望有帮助!
这篇关于如何在javascript中复制变量的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文