javascript - 使用 javascript/jQuery 复制 div 文本内容

标签 javascript jquery html wordpress shortcode

有很多关于此问题的帖子,但在花了几个小时尝试调整现有的解决方案后,我很清楚这有点复杂。

我正在尝试创建“复制到剪贴板”功能,以便我们的用户只需单击一下即可复制其序列号(我设法通过两种不同的解决方案实现此功能),但有一个主要问题。

由于序列号是使用短代码动态生成的,因此我无法将其放置在 HTML“文本”/“值”字段中,如下所示:

<input id="serial" value="[shortcode here]">

因为这破坏了短代码,所以它必须放置在它自己的 div 中,我是这样做的:

<div id="serial">[shortcode here]</div>

这会在页面上输出直接父 ID 为“serial”的短代码,因此我使用的 JS 应从元素 ID 中选择文本 - #serial

不幸的是,它没有......

<小时/>

我也尝试过采用这种方法,但没有成功:

来自自己动手部分:https://www.sitepoint.com/javascript-copy-to-clipboard/

这个可以使用纯文本,但不能使用短代码或自定义 div。

<小时/>

任何人都可以为我提供一个工作剪贴板解决方案,如上面的示例所示,并且不会破坏短代码?

最佳答案

document.getElementById("copyButton").addEventListener("click", function() {
    copyToClipboardMsg(document.getElementById("copyTarget"), "msg");
});

document.getElementById("copyButton2").addEventListener("click", function() {
    copyToClipboardMsg(document.getElementById("copyTarget2"), "msg");
});

document.getElementById("pasteTarget").addEventListener("mousedown", function() {
    this.value = "";
});


function copyToClipboardMsg(elem, msgElem) {
	  var succeed = copyToClipboard(elem);
    var msg;
    if (!succeed) {
        msg = "Copy not supported or blocked.  Press Ctrl+c to copy."
    } else {
        msg = "Text copied to the clipboard."
    }
    if (typeof msgElem === "string") {
        msgElem = document.getElementById(msgElem);
    }
    msgElem.innerHTML = msg;
    setTimeout(function() {
        msgElem.innerHTML = "";
    }, 2000);
}

function copyToClipboard(elem) {
	  // create hidden text element, if it doesn't already exist
    var targetId = "_hiddenCopyText_";
    var isInput = elem.tagName === "INPUT" || elem.tagName === "TEXTAREA";
    var origSelectionStart, origSelectionEnd;
    if (isInput) {
        // can just use the original source element for the selection and copy
        target = elem;
        origSelectionStart = elem.selectionStart;
        origSelectionEnd = elem.selectionEnd;
    } else {
        // must use a temporary form element for the selection and copy
        target = document.getElementById(targetId);
        if (!target) {
            var target = document.createElement("textarea");
            target.style.position = "absolute";
            target.style.left = "-9999px";
            target.style.top = "0";
            target.id = targetId;
            document.body.appendChild(target);
        }
        target.textContent = elem.textContent;
    }
    // select the content
    var currentFocus = document.activeElement;
    target.focus();
    target.setSelectionRange(0, target.value.length);
    
    // copy the selection
    var succeed;
    try {
    	  succeed = document.execCommand("copy");
    } catch(e) {
        succeed = false;
    }
    // restore original focus
    if (currentFocus && typeof currentFocus.focus === "function") {
        currentFocus.focus();
    }
    
    if (isInput) {
        // restore prior selection
        elem.setSelectionRange(origSelectionStart, origSelectionEnd);
    } else {
        // clear temporary content
        target.textContent = "";
    }
    return succeed;
}
<input id="copyTarget" value="Some initial text"> <button id="copyButton">Copy</button><br><br>
<span id="copyTarget2">Some Other Text</span> <button id="copyButton2">Copy</button><br><br>
<input id="pasteTarget"> Click in this Field and hit Ctrl+V to see what is on clipboard<br><br>
<span id="msg"></span><br>

请尝试上面的代码片段吗?

关于javascript - 使用 javascript/jQuery 复制 div 文本内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43892742/

相关文章:

javascript - 当我尝试从 Firestore 获取集合时出现问题

javascript - 如何默认将 NativeScript 设置为 RTL 布局

jquery - 使用位于 App_Data 中的静态 .json 文件?

JQUERY – 如何从中心调整图像大小(居中注册点)

jquery - 调整图表显示的容器宽度时出现 Highcharts 错误 19 问题

javascript - focus() 在 IE 中不起作用

javascript - grunt 替换时出现目标未定义错误

javascript - 如何使用 Leaflet 通过缩放更改 geojson 中的图标?

jquery + css 添加函数调用。

html - 什么是 HTML <a></a> 标签默认显示类型?