复制到剪贴板 javascript navigator.clipboard.write
这是一个颜色选择器随机生成器。
单击按钮时,它不会将十六进制代码复制到剪贴板。
它给出错误
Uncaught typeerror copyText.select is not function
复制文本:未定义 copytext[object HTMLSpanElement]
navigator​.​clipboard​.​write​Text 或 navigator​.​clipboard​.​write​
const hex = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "A", "B", "C", "D", "E", "F"];
const btn = document.getElementById("button");
const color = document.querySelector(".color");
btn.addEventListener("click", function() {
let hexColor = "#";
for (let i = 0; i < 6; i++) {
hexColor += hex[getRandomNumber()];
}
color.textContent = hexColor;
document.body.style.backgroundColor = hexColor;
});
function getRandomNumber() {
return Math.floor(Math.random() * hex.length);
}
function myFunction() {
let copyText = document.getElementById("color-code");
console.log("copytext" + copyText);
copyText.select();
navigator.clipboard.write(copyText.value);
alert("Copied the text: " + copyText.value);
}
<section class="hero is-large">
<div class="hero-body">
<div class="container has-text-centered">
<div class="columns is-mobile">
<div class="column is-half is-offset-one-quarter">
<div class="card">
<div class="card-header field is-grouped">
<h3 class="card-header-title is-inline">
<span> Background Color <span class="tag is-large is-pulled-right color" id="color-code">hexcode </span>
</span>
</h3>
<button class="button p-4 m-4" id="copyColorHex" onclick="myFunction()">
<span class="icon is-small is-right">
<svg xmlns="http://www.w3.org/2000/svg" class="w-3 h-3 " fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2" />
</svg>
</span>
</button>
</div>
</div>
</div>
</div>
</div>
<div class="container has-text-centered">
<div class="p-5 ">
<button class="button " id="button">click me</button>
</div>
</div>
</div>
</section>
-
select()
仅适用于<input>
和<textarea>
元素。您可以创建<input readonly>
来保存颜色输出(在其value
属性中,而不是<span>
的内容中),或者您可以使用以下方法选择<span>
元素的文本:const range = new Range(); range.selectNode(elementToSelect); getSelection().removeAllRanges(); getSelection().addRange(range);
-
value
也仅在某些表单元素上可用。它未针对<span>
元素定义,因此它给出值undefined
。您可以使用textContent
获取<span>
或大多数其他元素(但不是<input>
元素)的内容。 -
看起来您可能混淆了剪贴板复制策略。
navigator.clipboard.write
和writeText
不需要您先选择文本。但是如果您这样做是为了支持不支持异步剪贴板 API 的浏览器,那也没问题。writeText
就是您想要的,因为它接受一个字符串,而write
接受一个ClipboardItem
数组。(writeText
也具有更好的浏览器支持。)
放在一起:
const textToCopy = span.textContent;
if (navigator.clipboard && navigator.clipboard.writeText) {
navigator.clipboard.writeText(textToCopy).then(function() {
alert("Copied to clipboard:", textToCopy);
}, function(error) {
alert("Failed to copy to clipboard.\n\n" + error);
});
} else {
// fallback for older browsers
const range = new Range();
range.selectNode(span);
getSelection().removeAllRanges();
getSelection().addRange(range);
if (document.execCommand("Copy")) {
alert("Copied to clipboard:", textToCopy);
} else {
alert("Failed to copy to clipboard.");
}
}
const hex = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "A", "B", "C", "D", "E", "F"];
const btn = document.getElementById("button");
const color = document.querySelector(".color");
btn.addEventListener("click", function() {
let hexColor = "#";
for (let i = 0; i < 6; i++) {
hexColor += hex[getRandomNumber()];
}
color.textContent = hexColor;
document.body.style.backgroundColor = hexColor;
});
function getRandomNumber() {
return Math.floor(Math.random() * hex.length);
}
function myFunction() {
let copyText = document.getElementById("color-code");
var input = document.createElement("textarea");
input.value = copyText.textContent;
document.body.appendChild(input);
input.select();
navigator.clipboard.write(input.value);
alert("Copied the text: " + input.value);
input.remove();
}
<section class="hero is-large">
<div class="hero-body">
<div class="container has-text-centered">
<div class="columns is-mobile">
<div class="column is-half is-offset-one-quarter">
<div class="card">
<div class="card-header field is-grouped">
<h3 class="card-header-title is-inline">
<span> Background Color <span class="tag is-large is-pulled-right color" id="color-code">hexcode </span>
</span>
</h3>
<button class="button p-4 m-4" id="copyColorHex" onclick="myFunction()">
<span class="icon is-small is-right">
copy color
</span>
</button>
</div>
</div>
</div>
</div>
</div>
<div class="container has-text-centered">
<div class="p-5 ">
<button class="button " id="button">click me</button>
</div>
</div>
</div>
</section>
错误发生的原因
select 仅适用于
input、textarea、select box、option、optgroup、fieldset
,不适用于
任何 block 元素和 span 元素
,您可以在
此处
阅读有关 select 的更多信息>
并且按照我上面的代码,我在这里创建
textarea
并向其中添加颜色代码,在调用
select()
之后,我将其从 dom 元素中删除