我想要复制剪贴板中输入字段的内容。
我的代码是
<button class="btn btn-default btn-xs copy-link" title="Copy Link"><i class="fa fa-copy"></i></button>
<input type="text" class="link" value="Hello World">
<script>
$(document).on('click','.copy-link', function() {
var copyText = $(this).siblings('.link');
copyText.select();
document.execCommand("copy");
alert("Copied the text: " + copyText.val());
});
</script>
这个密码很好..。
但我要隐藏输入字段。喜欢
<button class="btn btn-default btn-xs copy-link" title="Copy Link"><i class="fa fa-copy"></i></button>
<input type="hidden" class="link" value="Hello World" />
那么这个值就不是用来处理同一个jquery的..。
还有什么是我遗漏的吗?
发布于 2018-06-05 02:00:47
如果您可以将链接放入按钮中的链接属性,
我建议你如下:
input
下面是一个工作片段:
$(document).on('click', '.copy-link', function() {
var link = $(this).attr('link');
var tempInput = document.createElement("input");
tempInput.style = "position: absolute; left: -1000px; top: -1000px";
tempInput.value = link;
document.body.appendChild(tempInput);
tempInput.select();
document.execCommand("copy");
console.log("Copied the text:", tempInput.value);
document.body.removeChild(tempInput);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.13/css/all.css" integrity="sha384-DNOHZ68U8hZfKXOrtjWvjxusGo9WQnrNx2sqG0tfsghAvtVlRW3tvkXWZh58N9jp" crossorigin="anonymous">
<button class="btn btn-default btn-xs copy-link" title="Copy Link" link="Hello World"><i class="fa fa-copy"></i></button>
希望能帮上忙。
发布于 2018-06-05 01:23:28
尝试不透明度0;在这里它肯定会工作;)
<input type="text" class="link hidden" value="Hello World 2">
.hidden {
position:absolute; top:-50px; opacity:0;
}
发布于 2018-06-05 01:25:58
应用下面提到的样式,然后使用相同的jquery代码。看起来不错。
<input type="text" class="link hidden" value="Hello World 2" style="position: absolute; left: -1000px;">
https://stackoverflow.com/questions/50696276
复制相似问题