Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

希望添加复制代码功能 #105

Open
Lee981265 opened this issue Jan 17, 2019 · 2 comments
Open

希望添加复制代码功能 #105

Lee981265 opened this issue Jan 17, 2019 · 2 comments
Labels
suggest 建议

Comments

@Lee981265
Copy link

Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

Describe the solution you'd like
A clear and concise description of what you want to happen.

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

Additional context
Add any other context or screenshots about the feature request here.

@shenliyang
Copy link
Owner

建议已收到,谢谢

@shenliyang shenliyang added the suggest 建议 label Jan 19, 2019
@spygg
Copy link

spygg commented Apr 25, 2022

在head.ejs中添加如下代码
js代码

<script type="text/javascript">
function copyToClip(event) {
    let cb = event.target.parentNode;
    var cls = cb.getElementsByClassName("line");
    var pt = ""
    for (var k = 0; k < cls.length; k++) {
        var cl = (cls[k].textContent || cls[k].innerHTML);
        cl = cl.replace('<span class="css"></span>', "\n")
        cl = cl.replace('<span class="javascript"></span>', "\n")
        pt += cl
        pt += "\n";
    }
    const textarea = document.createElement('textarea');
    textarea.value = pt;
    document.body.appendChild(textarea);
    textarea.select();
    if (document.execCommand('copy')) {
        document.execCommand('copy');
    }
    document.body.removeChild(textarea);
    alert("复制成功");
}

function doAddCopyCode() {
    var codeBlocks = document.getElementsByClassName('code');
    for (var i = 0; i < codeBlocks.length; i++) {
        //创建一个div
        var divCopy = document.createElement("div");
        divCopy.innerHTML = "点击复制"
        //为div创建属性class = "test"
        var divattr = document.createAttribute("class");
        divattr.value = "copy_my_code";

        //把属性class = "test"添加到div
        divCopy.setAttributeNode(divattr);
        codeBlocks[i].appendChild(divCopy)

        var code = codeBlocks[i].getElementsByTagName("pre")[1];

        codeBlocks[i].onclick = (e) => {
            copyToClip(e);

        }
    }
}

window.addEventListener('load', function () {
    doAddCopyCode();
}, false);
</script>

样式添加

<style type="text/css">
    .copy_my_code {
        position: absolute;
        top: 0;
        right: 0;
        border: 1px solid;
        background: #555;
        color: #fff;
        margin: 1px;
        padding: 0px 5px;
        cursor: pointer;
    }
</style>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
suggest 建议
Projects
None yet
Development

No branches or pull requests

3 participants