Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/74.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
Jquery 将多个项目复制到剪贴板_Jquery_Html_Css_Copy_Clipboard - Fatal编程技术网

Jquery 将多个项目复制到剪贴板

Jquery 将多个项目复制到剪贴板,jquery,html,css,copy,clipboard,Jquery,Html,Css,Copy,Clipboard,我正在复制到剪贴板,但它只拾取第一个ID。我理解这是因为当我将代码更改为文档时,您不能有多个ID。getElementsByClassName它不起作用 下面是我的代码 function CopyToClipboard() { var input = document.getElementById("toClipboard"); var textToClipboard = input.value; var success = true;

我正在复制到剪贴板,但它只拾取第一个ID。我理解这是因为当我将代码更改为
文档时,您不能有多个ID。getElementsByClassName
它不起作用

下面是我的代码

 function CopyToClipboard() {
        var input = document.getElementById("toClipboard");
        var textToClipboard = input.value;

        var success = true;
        if (window.clipboardData) { // Internet Explorer
            window.clipboardData.setData("Text", textToClipboard);
        }
        else {
            // create a temporary element for the execCommand method
            var forExecElement = CreateElementForExecCommand(textToClipboard);

            /* Select the contents of the element
                (the execCommand for 'copy' method works on the selection) */
            SelectContent(forExecElement);

            var supported = true;

            // UniversalXPConnect privilege is required for clipboard access in Firefox
            try {
                if (window.netscape && netscape.security) {
                    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
                }

                // Copy the selected content to the clipboard
                // Works in Firefox and in Safari before version 5
                success = document.execCommand("copy", false, null);
            }
            catch (e) {
                success = false;
            }

            // remove the temporary element
            document.body.removeChild(forExecElement);
        }

        if (success) {
            alert("The text is on the clipboard");
        }
        else {
            alert("Your browser doesn't allow clipboard access!");
        }
    }

    function CreateElementForExecCommand(textToClipboard) {
        var forExecElement = document.createElement("div");
        // place outside the visible area
        forExecElement.style.position = "absolute";
        forExecElement.style.left = "-10000px";
        forExecElement.style.top = "-10000px";
        // write the necessary text into the element and append to the document
        forExecElement.textContent = textToClipboard;
        document.body.appendChild(forExecElement);
        // the contentEditable mode is necessary for the  execCommand method in Firefox
        forExecElement.contentEditable = true;

        return forExecElement;
    }

    function SelectContent(element) {
        // first create a range
        var rangeToSelect = document.createRange();
        rangeToSelect.selectNodeContents(element);

        // select the contents
        var selection = window.getSelection();
        selection.removeAllRanges();
        selection.addRange(rangeToSelect);
    }
这是我的html

<div id="App-Details"><span>Activation Guid</span><br /><br />
<input id="toClipboard" value="@appDetails.ActivationGuid"> <button onclick='CopyToClipboard ()'>Copy Guid</button></div>
激活Guid

复制Guid
谢谢