Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Wordpress JS复制文本脚本_Javascript_Php_Jquery_Html_Wordpress - Fatal编程技术网

Javascript Wordpress JS复制文本脚本

Javascript Wordpress JS复制文本脚本,javascript,php,jquery,html,wordpress,Javascript,Php,Jquery,Html,Wordpress,我想创建一个文本区域,您可以将其内容复制到剪贴板。我在这块板上找到了一个解释代码的脚本,但我很难让它在wordpress上运行。按钮无法正确执行操作,没有任何内容复制到剪贴板。我做的唯一不同的事情就是文本上的标签。原始脚本使用,我使用。我只在页面上运行这个JS脚本。它与主题无关 JS HTML 不是因为我很聪明,只是因为我在问题上呆的时间更长。阿尔伯特·爱因斯坦 复制与共享 刚刚在wordpress主题上试用过-效果很好。您是否将脚本排队?如果没有,您如何将其包括在内?我没有将其添加到wp_en

我想创建一个文本区域,您可以将其内容复制到剪贴板。我在这块板上找到了一个解释代码的脚本,但我很难让它在wordpress上运行。按钮无法正确执行操作,没有任何内容复制到剪贴板。我做的唯一不同的事情就是文本上的标签。原始脚本使用
,我使用
。我只在页面上运行这个JS脚本。它与主题无关

JS

HTML

不是因为我很聪明,只是因为我在问题上呆的时间更长。阿尔伯特·爱因斯坦
复制与共享

刚刚在wordpress主题上试用过-效果很好。您是否将脚本排队?如果没有,您如何将其包括在内?我没有将其添加到wp_enqueue_脚本中。我使用了一个插件,可以将JS和CSS添加到页面中。
document.getElementById("copyButton").addEventListener("click", function() {
    copyToClipboard(document.getElementById("copyTarget"));
});

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;
}
    <textarea type="text" id="copyTarget" class="">It's not that I'm so smart, it's just that I stay with problems longer.Albert Einstein</textarea>
<button id="copyButton" type="button" class="btn btn-primary">Copy & Share</button>