问题是您试图在应该分配 HTML 节点的地方分配一个字符串。
“innerText”就是您要找的。
document.getElementById("copied-message").innerText = "Copied";
innerHTML 需要 DOM 中已存在的元素的实例。您可以通过克隆 DOM 中已有的元素并将innerHTMl 值设置为该元素的副本来测试这一点。
函数copyHtml() { var copyText = document.getElementById("代码输出").innerHTML;
// Create a temporary textarea element
var textarea = document.createElement("textarea");
// Set its value to the text you want to copy, with regular tags
textarea.value = copyText.replace(/</g, "<").replace(/>/g, ">");
// Append the textarea to the document
document.body.appendChild(textarea);
// Select the text and copy it to the clipboard
textarea.select();
document.execCommand("copy");
// Remove the temporary textarea
document.body.removeChild(textarea);
document.getElementById("copied-message").innerHTML = "Copied";
}
您可以使用此库更轻松地执行此类操作CopyShareify