如何复制我的网页中显示的带有嵌入按钮的代码

function copy() {

    var x= document.getElementById("content");

    x.select();

    document.execCommand('copy');  

    window.getSelection().removeAllRanges();

}

这是我用来选择和复制输入内容的js代码。


如果我想在我的网页上添加一个按钮来选择一些 div 并将显示的代码复制到剪贴板中,该怎么办。


慕田峪7331174
浏览 185回答 2
2回答

暮色呼如

在这里,我想出了我自己问题的解决方案。网址:<div id="divid" onclick="copy('divid')">Division to be copied</div>&nbsp;js:function copy(i)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var range = document.createRange();&nbsp; &nbsp; &nbsp; &nbsp; range.selectNode(document.getElementById(i));&nbsp; &nbsp; &nbsp; &nbsp; window.getSelection().removeAllRanges();&nbsp; &nbsp; &nbsp; &nbsp; window.getSelection().addRange(range);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; document.execCommand('copy');}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript