我正在将下面的 jQuery 代码转换为纯 JavaScript。这里和这里的代码笔是它的原始 jQuery 版本。
function CopyToClipboard(value, showNotification, notificationText) {
var $temp = $("<input>");
$("body").append($temp);
$temp.val(value).select();
document.execCommand("copy");
$temp.remove();
....
我的代码将其转换为 JavaScript。
function CopyToClipboard(value, showNotification, notificationText) {
var $temp = document.querySelector("<input>");
document.body.append($temp);
$temp.value.select();
document.execCommand("copy");
$temp.removeElement();
...
这是错误:
Uncaught SyntaxError: Failed to execute 'querySelector' on 'Document': '<input>' is not a valid selector.
编辑:
function CopyToClipboard(value, showNotification, notificationText) {
const inputElem = document.createElement('input');
inputElem.value = value;
var $temp = document.querySelector("<input>");
document.body.append($temp);
$temp.select();
document.execCommand("copy");
document.body.removeChild(el);
MM们
守着星空守着你
相关分类