无法自定义“复制到剪贴板”按钮

3
我已经在代码框中添加了复制到剪贴板按钮。问题是我想要将按钮定位到右上角,但我无法自定义它的位置,而且文字超出了代码框,尝试了word-break但没有效果。按钮功能不正常,请帮忙解决。强调:我不想使用overflow: hidden属性。

     
    function selectElementContents(el) {
    // Copy textarea, pre, div, etc.
    if (document.body.createTextRange) {
        // Internet Explorer
        var textRange = document.body.createTextRange();
        textRange.moveToElementText(el);
        textRange.select();
        textRange.execCommand("Copy");
    }
    else if (window.getSelection && document.createRange) {
        // Non-Internet Explorer
        var range = document.createRange();
        range.selectNodeContents(el);
        var sel = window.getSelection();
        sel.removeAllRanges();
        sel.addRange(range);
        try {
            var successful = document.execCommand('copy');
            var msg = successful ? 'successful' : 'unsuccessful';
            console.log('Copy command was ' + msg);
        }
        catch (err) {
            console.log('Oops, unable to copy');
        }
    }
} // end function selectElementContents(el)

function make_copy_button(el) {
    var copy_btn = document.createElement('input');
    copy_btn.type = "button";
    el.parentNode.insertBefore(copy_btn, el.nextSibling);
    copy_btn.onclick = function() {
        selectElementContents(el);
    };

    if (document.queryCommandSupported("copy") || parseInt(navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./)[2]) >= 42) {
        // Copy works with Internet Explorer 4+, Chrome 42+, Firefox 41+, Opera 29+
        copy_btn.value = "Copy to Clipboard";
    }
    else {
        // Select only for Safari and older Chrome, Firefox and Opera
        copy_btn.value = "Select All (then press Ctrl + C to Copy)";
    }
}
/* Note: document.queryCommandSupported("copy") should return "true" on browsers that support copy,
    but there was a bug in Chrome versions 42 to 47 that makes it return "false".  So in those
    versions of Chrome feature detection does not work!
    See https://code.google.com/p/chromium/issues/detail?id=476508
*/

make_copy_button(document.getElementById("markup"));
.codebox { background-color: #f3f3f6; border-radius: .25rem; padding: 2.25rem 2.25rem 2.25rem 2.5rem; margin: 1.2rem 0 1.2rem; border: 2px solid #d5d5d8; position: relative; } 
.codebox:before{ content: ''; position: absolute; background-image: url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA1MiA4Ij4KICA8cGF0aCBkPSJNNTIgN2MtMy4yIDAtMy4yLTMtNi41LTMtMy4yIDAtMy4yIDMtNi41IDNzLTMuMy0zLTYuNS0zYy0zLjMgMC0zLjMgMy02LjUgM3MtMy4yLTMtNi41LTNjLTMuMiAwLTMuMiAzLTYuNSAzLTMuMiAwLTMuMi0zLTYuNS0zUzMuMyA3IDAgN3YxaDUyVjd6IiBmaWxsLXJ1bGU9ImV2ZW5vZGQiIGNsaXAtcnVsZT0iZXZlbm9kZCIgZmlsbD0iI2YzZjNmNiIvPgogIDxwYXRoIGQ9Ik01MiA2Yy0zLjIgMC0zLjItMy02LjUtMy0zLjIgMC0zLjIgMy02LjUgM3MtMy4zLTMtNi41LTNjLTMuMyAwLTMuMyAzLTYuNSAzcy0zLjItMy02LjUtM2MtMy4yIDAtMy4yIDMtNi41IDMtMy4yIDAtMy4yLTMtNi41LTNTMy4zIDYgMCA2IiBmaWxsPSJub25lIiBzdHJva2U9IiNkNWQ1ZDgiIHN0cm9rZS13aWR0aD0iMiIvPgo8L3N2Zz4K); background-repeat: no-repeat; background-size: cover; top: -7px; left: 20px; width: 52px; height: 8px; } 
.codebox p{font-size:16px;margin-left: 29px;margin-bottom: 0;} 
.codebox p:before{ content: ''; width: 1.5rem;margin-left: 16px; height: 1.5rem;left: -4px; position: absolute; top: 21px; background-image: url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyNS41IDI1LjQiPgogIDxjaXJjbGUgY3g9IjEyLjgiIGN5PSIxMi43IiByPSIxMiIgZmlsbC1ydWxlPSJldmVub2RkIiBjbGlwLXJ1bGU9ImV2ZW5vZGQiIGZpbGw9IiNmOWVkNDMiIHN0cm9rZT0iI2Y5ZWQ0MyIgc3Ryb2tlLXdpZHRoPSIxLjIiLz4KICA8cGF0aCBkPSJNOC40IDhWNi40YzAtLjQuNC0uNy44LS43aDguNmMuNCAwIC43LjMuNy43djEyLjFjMCAuNC0uMy43LS43LjdIMTYiIGZpbGwtcnVsZT0iZXZlbm9kZCIgY2xpcC1ydWxlPSJldmVub2RkIiBmaWxsPSIjMzQ0NDRjIi8+CiAgPHBhdGggZD0iTTcuOCA3LjNoOC4xYy40IDAgLjcuMy43Ljd2MTEuNmMwIC40LS4zLjctLjcuN0g3LjhjLS40IDAtLjctLjMtLjctLjdWOC4xYy0uMS0uNC4zLS44LjctLjh6IiBmaWxsLXJ1bGU9ImV2ZW5vZGQiIGNsaXAtcnVsZT0iZXZlbm9kZCIgZmlsbD0iI2Y2OGM1MCIgc3Ryb2tlPSIjZjY4YzUwIiBzdHJva2Utd2lkdGg9Ii41Ii8+CiAgPHBhdGggZD0iTTE1LjIgMTJIOC41TTE1LjIgMTAuMkg4LjVNMTUuMiAxMy44SDguNU0xNS4yIDE1LjhIOC41TTExLjggMTcuNUg4LjUiIGZpbGw9Im5vbmUiIHN0cm9rZT0iI2Y5ZWQ0MyIgc3Ryb2tlLXdpZHRoPSIuOTQiIHN0cm9rZS1saW5lY2FwPSJyb3VuZCIvPgo8L3N2Zz4K); }
 
<pre id="markup">
                    <div class="codebox"><p>
                        Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.
    </div>
    </pre>


你是不是想说溢出而不是概述?我没有看到任何试图移动输入元素的样式 - 我错过了什么吗? - A Haworth
是的,是的,我的意思是溢出了吗?你能帮忙解决吗? - ASHISH KUMAR
1个回答

1
为了让用户查看所有代码,您可以使用MDN中定义的选项之一。
white-space: pre-wrap

看起来这就是你所需要的。

如果要移动按钮,您可能希望将其放置在右上方,也许留有一些边距,具体取决于您。

此代码段使用CSS添加了空白并移动了输入按钮。

function selectElementContents(el) {
    // Copy textarea, pre, div, etc.
    if (document.body.createTextRange) {
        // Internet Explorer
        var textRange = document.body.createTextRange();
        textRange.moveToElementText(el);
        textRange.select();
        textRange.execCommand("Copy");
    }
    else if (window.getSelection && document.createRange) {
        // Non-Internet Explorer
        var range = document.createRange();
        range.selectNodeContents(el);
        var sel = window.getSelection();
        sel.removeAllRanges();
        sel.addRange(range);
        try {
            var successful = document.execCommand('copy');
            var msg = successful ? 'successful' : 'unsuccessful';
            console.log('Copy command was ' + msg);
        }
        catch (err) {
            console.log('Oops, unable to copy');
        }
    }
} // end function selectElementContents(el)

function make_copy_button(el) {
    var copy_btn = document.createElement('input');
    copy_btn.type = "button";
    el.parentNode.insertBefore(copy_btn, el);
    copy_btn.onclick = function() {
        selectElementContents(el.querySelector('.codebox'));
    };

    if (document.queryCommandSupported("copy") || parseInt(navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./)[2]) >= 42) {
        // Copy works with Internet Explorer 4+, Chrome 42+, Firefox 41+, Opera 29+
        copy_btn.value = "Copy to Clipboard";
    }
    else {
        // Select only for Safari and older Chrome, Firefox and Opera
        copy_btn.value = "Select All (then press Ctrl + C to Copy)";
    }
}
/* Note: document.queryCommandSupported("copy") should return "true" on browsers that support copy,
    but there was a bug in Chrome versions 42 to 47 that makes it return "false".  So in those
    versions of Chrome feature detection does not work!
    See https://code.google.com/p/chromium/issues/detail?id=476508
*/

make_copy_button(document.getElementById("markup"));
.codebox { background-color: #f3f3f6; border-radius: .25rem; padding: 2.25rem 2.25rem 2.25rem 2.5rem; margin: 1.2rem 0 1.2rem; border: 2px solid #d5d5d8; position: relative; } 
.codebox:before{ content: ''; position: absolute; background-image: url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA1MiA4Ij4KICA8cGF0aCBkPSJNNTIgN2MtMy4yIDAtMy4yLTMtNi41LTMtMy4yIDAtMy4yIDMtNi41IDNzLTMuMy0zLTYuNS0zYy0zLjMgMC0zLjMgMy02LjUgM3MtMy4yLTMtNi41LTNjLTMuMiAwLTMuMiAzLTYuNSAzLTMuMiAwLTMuMi0zLTYuNS0zUzMuMyA3IDAgN3YxaDUyVjd6IiBmaWxsLXJ1bGU9ImV2ZW5vZGQiIGNsaXAtcnVsZT0iZXZlbm9kZCIgZmlsbD0iI2YzZjNmNiIvPgogIDxwYXRoIGQ9Ik01MiA2Yy0zLjIgMC0zLjItMy02LjUtMy0zLjIgMC0zLjIgMy02LjUgM3MtMy4zLTMtNi41LTNjLTMuMyAwLTMuMyAzLTYuNSAzcy0zLjItMy02LjUtM2MtMy4yIDAtMy4yIDMtNi41IDMtMy4yIDAtMy4yLTMtNi41LTNTMy4zIDYgMCA2IiBmaWxsPSJub25lIiBzdHJva2U9IiNkNWQ1ZDgiIHN0cm9rZS13aWR0aD0iMiIvPgo8L3N2Zz4K); background-repeat: no-repeat; background-size: cover; top: -7px; left: 20px; width: 52px; height: 8px; } 
.codebox p{font-size:16px;margin-left: 29px;margin-bottom: 0;} 
.codebox p:before{ content: ''; width: 1.5rem;margin-left: 16px; height: 1.5rem;left: -4px; position: absolute; top: 21px; background-image: url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyNS41IDI1LjQiPgogIDxjaXJjbGUgY3g9IjEyLjgiIGN5PSIxMi43IiByPSIxMiIgZmlsbC1ydWxlPSJldmVub2RkIiBjbGlwLXJ1bGU9ImV2ZW5vZGQiIGZpbGw9IiNmOWVkNDMiIHN0cm9rZT0iI2Y5ZWQ0MyIgc3Ryb2tlLXdpZHRoPSIxLjIiLz4KICA8cGF0aCBkPSJNOC40IDhWNi40YzAtLjQuNC0uNy44LS43aDguNmMuNCAwIC43LjMuNy43djEyLjFjMCAuNC0uMy43LS43LjdIMTYiIGZpbGwtcnVsZT0iZXZlbm9kZCIgY2xpcC1ydWxlPSJldmVub2RkIiBmaWxsPSIjMzQ0NDRjIi8+CiAgPHBhdGggZD0iTTcuOCA3LjNoOC4xYy40IDAgLjcuMy43Ljd2MTEuNmMwIC40LS4zLjctLjcuN0g3LjhjLS40IDAtLjctLjMtLjctLjdWOC4xYy0uMS0uNC4zLS44LjctLjh6IiBmaWxsLXJ1bGU9ImV2ZW5vZGQiIGNsaXAtcnVsZT0iZXZlbm9kZCIgZmlsbD0iI2Y2OGM1MCIgc3Ryb2tlPSIjZjY4YzUwIiBzdHJva2Utd2lkdGg9Ii41Ii8+CiAgPHBhdGggZD0iTTE1LjIgMTJIOC41TTE1LjIgMTAuMkg4LjVNMTUuMiAxMy44SDguNU0xNS4yIDE1LjhIOC41TTExLjggMTcuNUg4LjUiIGZpbGw9Im5vbmUiIHN0cm9rZT0iI2Y5ZWQ0MyIgc3Ryb2tlLXdpZHRoPSIuOTQiIHN0cm9rZS1saW5lY2FwPSJyb3VuZCIvPgo8L3N2Zz4K); }

#markup {
  white-space: pre-wrap;
}

input {
  transform: translateX(calc(100vw - 100% - 50px));
  margin: 20px;
}
<pre id="markup">
                    <div class="codebox"><p>
                        Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.Here is your code.
    </div>
    </pre>

你需要检查白空格设置是否符合要求,还需要确保系统只选择代码文本而不是全部内容。
更新:我检查了被选中的区域,发现它获取了整个标记元素的内容,而不仅仅是代码框。现已进行更正:
    selectElementContents(el.querySelector('.codebox'));

在 make_copy_button 函数中。

更新2:要求将输入按钮放置在代码框上方,而不是在右上角。片段已被修改以更改HTML,使输入元素位于代码之前,而不是像最初一样在其后。CSS已被修改以将其移动到视口的右侧。您需要查看定位和边距,以决定您想要的确切位置。


1
哦!太好了。学到了新东西。但是有一件事,为什么复制按钮会选择代码框上方和下方的一行(超出代码框)?点击复制后,您将看到所选空间。 - ASHISH KUMAR
它选择的是整个标记元素的内容,而不仅仅是代码框部分。我已经加了一个更正。 - A Haworth
1
你不应该有多个具有相同id的元素 - 但是你可以有许多具有相同class的元素。例如,你是否有多个具有id为markup的元素? - A Haworth
1
我想添加两个代码框和两个复制按钮。要激活复制按钮,我必须使用这个<Pre id="markup">,并且不能有多个具有相同ID的元素。所以我尝试使用getElementsByClassName,但仍然没有找到解决方案。 - ASHISH KUMAR
我不明白,你不能有多个id="markup"的元素。 - A Haworth
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接