使用Javascript复制多个元素';将文本值保存到用户剪贴板
我在创建一个函数时遇到了问题,该函数允许某人单击不同的按钮并选择相应的使用Javascript复制多个元素';将文本值保存到用户剪贴板,javascript,jquery,copy,clipboard,paste,Javascript,Jquery,Copy,Clipboard,Paste,我在创建一个函数时遇到了问题,该函数允许某人单击不同的按钮并选择相应的标记,然后将所述标记中的文本复制到剪贴板以粘贴 <p class="copyableInput grey49" id="p7">#494949</p> <button class="copyableInputButton" onclick="copyToClipboard(p7)">COPY # </button> <p class="copyableInput grey6
标记,然后将所述
标记中的文本复制到剪贴板以粘贴
<p class="copyableInput grey49" id="p7">#494949</p>
<button class="copyableInputButton" onclick="copyToClipboard(p7)">COPY #
</button>
<p class="copyableInput grey66" id="p8">#666666</p>
<button class="copyableInputButton" onclick="copyToClipboard(p8)">COPY #
</button>
<p class="copyableInput greycc" id="p9">#cccccc</p>
<button class="copyableInputButton" onclick="copyToClipboard(p9)">COPY #
</button>
<p class="copyableInput greyf9" id="p10"><span
style="color:#494949">#f9f9f9</span></p>
<button class="copyableInputButton" onclick="copyToClipboard(p10)">COPY #
</button>
function copyToClipboard(target){
var copy= document.getElementById(target);
copy.select();
document.execCommand("Copy");
alert("Copied the text: " + copy.value);
}
\4949
抄袭#
#666666
抄袭#
#cccc
抄袭#
\f9f9
抄袭#
功能copyToClipboard(目标){
var copy=document.getElementById(目标);
copy.select();
文件。执行命令(“副本”);
警报(“复制文本:+copy.value”);
}
如果我忘了什么,请告诉我 当您单击按钮时,函数将获得段落标记:
<p id="p9" class="copyableInput greycc">
此功能适用于更多浏览器 看到这个了吗
非常感谢。这绝对有效。现在我只需要花一些时间来研究它,这样我就能真正理解到底发生了什么。再次感谢!
var copy = target.innerHTML
<p class="copyableInput grey49" id="p7">#494949</p>
<button class="copyableInputButton" onclick="copyToClipboard('p7')">COPY #</button>
<p class="copyableInput grey66" id="p8">#666666</p>
<button class="copyableInputButton" onclick="copyToClipboard('p8')">COPY #</button>
<p class="copyableInput greycc" id="p9">#cccccc</p>
<button class="copyableInputButton" onclick="copyToClipboard('p9')">COPY #</button>
<p class="copyableInput greyf9" id="p10"><span style="color:#494949">#f9f9f9</span></p>
<button class="copyableInputButton" onclick="copyToClipboard('p10')">COPY #</button>
function copyToClipboard(target) {
var element = document.getElementById(target);
var text = element.innerHTML;
CopyToClipboard(text);
alert("Copied the text");
}
function CopyToClipboard (text) {
// Copies a string to the clipboard. Must be called from within an
// event handler such as click. May return false if it failed, but
// this is not always possible. Browser support for Chrome 43+,
// Firefox 42+, Safari 10+, Edge and IE 10+.
// IE: The clipboard feature may be disabled by an administrator. By
// default a prompt is shown the first time the clipboard is
// used (per session).
if (window.clipboardData && window.clipboardData.setData) {
// IE specific code path to prevent textarea being shown while dialog is visible.
return clipboardData.setData("Text", text);
} else if (document.queryCommandSupported && document.queryCommandSupported("copy")) {
var textarea = document.createElement("textarea");
textarea.textContent = text;
textarea.style.position = "fixed"; // Prevent scrolling to bottom of page in MS Edge.
document.body.appendChild(textarea);
textarea.select();
try {
return document.execCommand("copy"); // Security exception may be thrown by some browsers.
} catch (ex) {
console.warn("Copy to clipboard failed.", ex);
return false;
} finally {
document.body.removeChild(textarea);
}
}
}