将文本复制到剪贴板的最佳方法是什么?(多浏览器)
我试过了:
function copyToClipboard(text) {
if (window.clipboardData) { // Internet Explorer
window.clipboardData.setData("Text", text);
} else {
unsafeWindow.netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
const clipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper);
clipboardHelper.copyString(text);
}
}
Run Code Online (Sandbox Code Playgroud)
但在Internet Explorer中,它会出现语法错误.在Firefox中,它说unsafeWindow is not defined.
没有闪存的好技巧:Trello如何访问用户的剪贴板?
如何将div中的文本复制到剪贴板?我有一个div,需要添加一个链接,将文本添加到剪贴板.这有解决方案吗?
<p class="content">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</p>
<a class="copy-text">copy Text</a>
Run Code Online (Sandbox Code Playgroud)
单击复制文本后,我按Ctrl+ V,必须粘贴.
我知道这里有很多次问过这个问题,包括: 如何用JavaScript复制到剪贴板?或者如何使用jQuery将文本复制到客户端的剪贴板?,我缩小了范围:
条件:
有这样的解决方案或解决方法吗?
当前代码附加一个按钮,可以快速选择<pre>标记中的某些代码.我想要添加的是能够将该内容复制到剪贴板并将按钮文本更改为"复制".
如何通过修改下面的当前工作代码来实现它?我不介意使用clipboard.js,jQuery位或者只是原生的JS支持,因为它是从Chrome 43开始引入的.我只是不知道如何继续添加我需要的东西.
function selectPre(e) {
if (window.getSelection) {
var s = window.getSelection();
if (s.setBaseAndExtent) {
s.setBaseAndExtent(e, 0, e, e.innerText.length - 1);
}
else {
var r = document.createRange();
r.setStart(e.firstChild, 0);
r.setEnd(e.lastChild, e.lastChild.textContent.length);
s.removeAllRanges();
s.addRange(r);
}
}
else if (document.getSelection) {
var s = document.getSelection();
var r = document.createRange();
r.selectNodeContents(e);
s.removeAllRanges();
s.addRange(r);
}
else if (document.selection) {
var r = document.body.createTextRange();
r.moveToElementText(e);
r.select();
}
}
var diff = document.getElementById('diff_table').getElementsByTagName('tr');
var difflen = diff.length;
for(i=0; i<difflen; i++) {
var …Run Code Online (Sandbox Code Playgroud) 如何复制div标签中的内容并将其粘贴到 Paint、Powerpoint 等应用程序中。
我有一个搜索表单,可将商人详细信息(每个商人的姓名,电话,电子邮件地址)输出到表格中。
我希望在这些字段中的每个字段旁边都有一个复制按钮,以便用户可以单击它并将其复制到剪贴板(复制时文本突出显示)。我的用户将仅使用IE9浏览。
问题在于可能会有不止一组结果,因此脚本无法调用特定的编号函数,就像我对以下textarea所做的那样:
function highlightmetasearch01() {
document.copydata01.text2copy01.select();
document.copydata01.text2copy01.focus();
}
function copymetasearch01() {
highlightmetasearch01();
textRange = document.copydata01.text2copy01.createTextRange();
textRange.execCommand("RemoveFormat");
textRange.execCommand("Copy");
}
function highlightmetasearch02() {
document.copydata02.text2copy02.select();
document.copydata02.text2copy02.focus();
}
function copymetasearch02() {
highlightmetasearch02();
textRange = document.copydata02.text2copy02.createTextRange();
textRange.execCommand("RemoveFormat");
textRange.execCommand("Copy");
}
Run Code Online (Sandbox Code Playgroud)
HTML:
<textarea name="text2copy01">The first textarea.</textarea>
<br />
<button onclick="copymetasearch01();return false;">COPY</button>
<textarea name="text2copy02">The second textarea.</textarea>
<br />
<button onclick="copymetasearch02();return false;">COPY</button>
Run Code Online (Sandbox Code Playgroud)
我想知道这是否可能?...
<td><span>Name from DB here</span> <button onclick="<!--copy and highlight text within most recent span tag-->">COPY</button></td>
<td><span>Phone from DB here</span> <button onclick="<!--copy and highlight text within most …Run Code Online (Sandbox Code Playgroud) 正如标题所说,我使用了这个(例子)更具体地说,我使用了右上角的信息框(美国人口密度).我修改它以显示更多信息onclick等.现在我有一个问题,这个div是不可选择的.我需要能够选择文本(用于复制文本),而不是选择文本,它只是移动地图.是否有一个"非常简单的解决方案",因为我找不到合适的地方.也许在某种程度上CSS?
.info {
position: fixed;
right: 0;
bottom: 23px;
padding: 6px 8px;
font: 14px/16px Arial, Helvetica, sans-serif;
background: white;
background: rgba(255,255,255,0.8);
box-shadow: 0 0 15px rgba(0,0,0,0.2);
border-radius: 5px;
/*-webkit-user-select: text;
-moz-user-select: text;
user-select: text;
-webkit-user-drag: text;*/ /*Tried this, didn't work. */
}
.info h4 {
margin: 0 0 100px;
color: #777;
}
Run Code Online (Sandbox Code Playgroud)
编辑:我应该添加,当我开始选择其他div时,我可以选择它.如图所示,如果我开始选择显示我的图层的div,我可以选择文本,但如果我尝试直接从该div中选择(在图片的右上角),它会移动地图.我知道画面很小,希望它能提供所需的信息/视觉.图片:http://www.upload.ee/image/3839164/asd.PNG
谢谢,Kristjan
我正在尝试遵循以下代码:https : //www.w3schools.com/howto/howto_js_copy_clipboard.asp
HTML模态:
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">App Link</h4>
</div>
<div class="modal-body">
<input id="appID" value="123"></input>
<button type="button" class="btn btn-success" id="copyBtn">Copy</button>
</div>
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
这是我的JS:
var copyBtn = document.getElementById('copyBtn');
copyBtn.onclick = function(){
var myCode = document.getElementById('appID').value;
var fullLink = document.createElement('input');
document.body.appendChild(fullLink);
fullLink.value = "http://fulllink/" + myCode;
fullLink.select();
document.execCommand("copy", false);
fullLink.remove();
alert("Copied the text: " + fullLink.value);
}
Run Code Online (Sandbox Code Playgroud)
Code will not …
javascript ×7
jquery ×4
clipboard ×3
copy-paste ×2
css ×2
clipboard.js ×1
html ×1
html5 ×1
leaflet ×1