将复制到剪贴板的jQuery代码转换为纯JavaScript

将复制到剪贴板的jQuery代码转换为纯JavaScript,javascript,html,jquery,Javascript,Html,Jquery,我正在将下面的jQuery代码转换为纯JavaScript。是它的原始jQuery版本 function CopyToClipboard(value, showNotification, notificationText) { var $temp = $("<input>"); $("body").append($temp); $temp.val(value).select(); document.execCom

我正在将下面的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);
以下是一个例子:

copyLink (url) {
    const el = document.createElement('textarea');
    el.value = url;
    document.body.appendChild(el);
    el.select();
    document.execCommand('copy');
    document.body.removeChild(el);
},

以下是一个例子:

copyLink (url) {
    const el = document.createElement('textarea');
    el.value = url;
    document.body.appendChild(el);
    el.select();
    document.execCommand('copy');
    document.body.removeChild(el);
},


嗯,对于您提到的代码部分,您需要在代码库中更改更多的代码

首先,您需要知道$将创建一个输入元素,在普通javascript中,它的等价物是document.createElementinput。 $temp.valvalue.select;将在一行中执行两个操作,首先设置功能值参数输入值,最后通过选择选择该值。因此,要将其转换为javascript,我们需要执行以下两个操作: $temp.value=价值; $temp.select; 最后,您需要使用remove而不是removeElement删除输入元素。 最终结果如下:

document.querySelectorcopymanager.addEventListenerclick,e=>{ e、 防止违约; CopyToClipboarddocument.location.href,true,复制值; }; 函数CopyToClipboardvalue、showNotification、notificationText{ var$temp=document.createElementinput; document.body.append$temp; $temp.value=价值; $temp.select; 文件副本; $temp.remove; 如果showNotification的类型===未定义{ showNotification=true; } 如果通知文本的类型===未定义{ notificationText=复制到剪贴板; } var notificationTag=document.createElementdiv; notificationTag.className=复制通知; notificationTag.innerText=notificationText; 如果显示通知{ document.body.appendnotificationTag; notificationTag.style.display=块; notificationTag.style.transition=不透明度1s; setTimeoutfunction{ notificationTag.style.opacity=0; notificationTag.remove; }, 1000; } } .副本通知{ 颜色:ffffff; 背景色:rgba0,0,0,0.8; 填充:20px; 边界半径:30px; 位置:固定; 最高:50%; 左:50%; 宽度:150px; 利润上限:-30px; 左边距:-85px; 显示:无; 文本对齐:居中; }
单击以复制到剪贴板那么,对于您提到的代码部分,您需要在代码库中更改更多的代码

首先,您需要知道$将创建一个输入元素,在普通javascript中,它的等价物是document.createElementinput。 $temp.valvalue.select;将在一行中执行两个操作,首先设置功能值参数输入值,最后通过选择选择该值。因此,要将其转换为javascript,我们需要执行以下两个操作: $temp.value=价值; $temp.select; 最后,您需要使用remove而不是removeElement删除输入元素。 最终结果如下:

document.querySelectorcopymanager.addEventListenerclick,e=>{ e、 防止违约; CopyToClipboarddocument.location.href,true,复制值; }; 函数CopyToClipboardvalue、showNotification、notificationText{ var$temp=document.createElementinput; document.body.append$temp; $temp.value=价值; $temp.select; 文件副本; $temp.remove; 如果showNotification的类型===未定义{ showNotification=true; } 如果通知文本的类型===未定义{ notificationText=复制到剪贴板; } var notificationTag=document.createElementdiv; notificationTag.className=复制通知; notificationTag.innerText=notificationText; 如果显示通知{ document.body.appendnotificationTag; notificationTag.style.display=块; notificationTag.style.transition=不透明度1s; setTimeoutfunction{ notificationTag.style.opacity=0; notificationTag.remove; }, 1000; } } .副本通知{ 颜色:ffffff; 背景色:rgba0,0,0,0.8; 填充:20px; 边界半径:30px; 位置:固定; 最高:50%; 左:50%; 宽度:150px; 利润上限:-30px; 左边距:-85px; 显示:无; 文本对齐:居中; }
单击以复制到剪贴簿是否需要类似document.createElementINPUT的内容?jQuery代码没有这样做,但我认为这是必要的。是的,我在选择document.createElement“input”之前添加了它,但它不起作用。我想我可能遗漏了一些东西我在jQuery的实现上面添加了一个代码笔链接你不想要像document.createElementINPUT这样的东西吗?jQuery代码没有这样做,但我认为这是必要的。是的,我在选择document.createElement“input”之前添加了它,但它不起作用。我想我可能遗漏了什么我在itI的jQuery实现上面添加了一个代码笔链接我创建了输入元素。将其附加到正文中。然后使用querySelector选择它,但它不起作用。请尝试将我的函数转换为纯JavaScriptor。您不能像这样使用querySelector。检查文档:请注意,您必须创建此元素,设置所需的值,
将其插入DOM,选择并复制。。。然后,删除。我完全按照你说的做了,请访问更新的问题。还是一样的错误。不,你有错误。我把它们修好了,但现在我不知道如何保存那支笔。几分钟后会给你链接,明白了。创建帐户。检查这里的固定代码:我创建了输入元素。将其附加到正文中。然后使用querySelector选择它,但它不起作用。请尝试将我的函数转换为纯JavaScriptor。您不能像这样使用querySelector。Check docs:P.S.您必须创建此元素,设置所需的值,将其插入DOM,选择并复制。。。然后,删除。我完全按照你说的做了,请访问更新的问题。还是一样的错误。不,你有错误。我把它们修好了,但现在我不知道如何保存那支笔。几分钟后会给你链接,明白了。创建帐户。检查此处的固定代码:请帮个忙,并将其余代码转换为javascript。@BasirPayenda我只是为您添加描述,这可能会有所帮助。@BasirPayenda无需担心,您现在可以在answer和live demo中查看最新更新。请帮个忙,并将其余代码转换为javascript。@BasirPayenda我只是为您添加描述,这可能会有所帮助。@BasirPayenda不用担心,您现在可以在answer和live demo中查看最新更新。