将文本复制到剪贴板的最佳方法是什么?(多浏览器)
我试过了:
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标签的内容......我们的想法是突出显示/选择所有文本,这样用户就不需要用鼠标手动突出显示文本了错过了一些文字?
例如,假设我们有一个DIV如下:
<div id="selectable">http://example.com/page.htm</div>
Run Code Online (Sandbox Code Playgroud)
...当用户点击任何该URL时,整个URL文本都会突出显示,以便他们可以轻松地在浏览器中拖动所选文本,或者通过右键单击复制完整的URL.
谢谢!
我有一个工作函数,它针对textarea并将内容复制到剪贴板.它直接针对textarea时效果很好.
我需要针对子组件中的textarea提供相同的功能.我无法弄清楚如何定位每个组件中的特定区域.
工作范例:
<div class="media-label col-md-12">Product Title:</div>
<textarea
class="col-md-6 col-md-offset-3"
v-model="productTitle"
id="productTitle"
></textarea>
<button
type="button"
class="btn btn-info"
data-copytarget="#productTitle"
v-on:click="copyTextArea"
>
Copy Title To Clipboard
</button>
Run Code Online (Sandbox Code Playgroud)
复制功能:
copyTextArea(e) {
var targetElement = e.target;
var copiedTarget = targetElement.dataset.copytarget;
var element = document.querySelector(copiedTarget);
element.select();
document.execCommand('copy');
},
Run Code Online (Sandbox Code Playgroud)
组件设置我遇到了以下问题:
<ExampleComponent
title="Title"
input-type="textarea"
v-model="productTitle"
id="productTitle"
></ExampleComponent>
<button
type="button"
class="btn btn-info"
copytarget="#productTitle"
v-on:click="copyTextArea"
>
Copy Title To Clipboard
</button>
<ExampleComponent
title="Description"
input-type="textarea"
v-model="productTitle"
id="productTitle"
></ExampleComponent>
<button
type="button"
class="btn btn-info"
copytarget="#productTitle"
v-on:click="copyTextArea"
>
Copy Title To Clipboard
</button>
Run Code Online (Sandbox Code Playgroud)