博客园添加复制代码

效果如下:


此处为代码区域pacinonono

需要将整段代码粘贴入编辑框内:
//<!-- 添加复制按钮 --> //<button onclick="copyCode()">复制代码</button> //<pre> //<code id="codeSnippet"> //此处为代码区域pacinonono //</code> //</pre> //<script> //function copyCode() { // var code = document.getElementById("codeSnippet").innerText; // var textArea = document.createElement("textarea"); // textArea.value = code; // document.body.appendChild(textArea); // textArea.select(); // document.execCommand("copy"); // document.body.removeChild(textArea); // alert("代码已复制!"); //} //</script>
需要在每个//处换行
然后替换“..复制区域”为目标内容。

同理,下面是另一种格式:


此处为代码区域pacinonono

//<pre> //<code id="codeSnippet"> //此处为代码区域pacinonono //</code> //</pre> //<script> //function copyCode() { // var code = document.getElementById("codeSnippet").innerText; // navigator.clipboard.writeText(code).then(function() { // alert("代码已复制!"); // }).catch(function(error) { // alert("复制失败:" + error); // }); //} //</script>
个人比较喜欢后者..

posted @ 2025-03-13 20:15  Pacinonono  阅读(59)  评论(0)    收藏  举报