跳转到主要内容
你行网

Main navigation

  • 首页
  • 文章
  • 教程
  • 视频
  • 下载
  • 社区
  • 网站导航
User account menu
  • 登录

面包屑

  1. 首页

代码框右上角的复制功能怎么实现

由 hrs, 16 三月, 2025

如图上图,

要在代码框右上角实现复制功能,通常需要结合 HTML、CSS 和 JavaScript 来完成。以下是一个简单的实现示例:

1. HTML 结构


<div class="code-container">
   <pre><code id="code">
$int = 42;          // integer
$float = 3.14;      // float
$string = "Hello";  // string
$bool = true;       // boolean
$array = [1, 2, 3]; // array
$object = new stdClass(); // object
$null = null;       // null
$resource = fopen("file.txt", "r"); // resource
$callable = function() { return "Hello"; }; // callable
   </code></pre>
   <button class="copy-button" onclick="copyCode()">复制</button>
</div>


运行 HTML
2. CSS 样式


.code-container {
   position: relative;
   background: #f4f4f4;
   padding: 10px;
   border: 1px solid #ddd;
   border-radius: 4px;
}
pre {
   margin: 0;
}
.copy-button {
   position: absolute;
   top: 10px;
   right: 10px;
   background: #007bff;
   color: white;
   border: none;
   padding: 5px 10px;
   border-radius: 4px;
   cursor: pointer;
}
.copy-button:hover {
   background: #0056b3;
}


3. JavaScript 功能


function copyCode() {
   const code = document.getElementById('code').innerText;
   navigator.clipboard.writeText(code).then(() => {
       alert('代码已复制到剪贴板!');
   }).catch(err => {
       console.error('复制失败: ', err);
   });
}


4. 完整示例


<!DOCTYPE html>
<html lang="zh-CN">
<head>
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>代码复制示例</title>
   <style>
       .code-container {
           position: relative;
           background: #f4f4f4;
           padding: 10px;
           border: 1px solid #ddd;
           border-radius: 4px;
       }
       pre {
           margin: 0;
       }
       .copy-button {
           position: absolute;
           top: 10px;
           right: 10px;
           background: #007bff;
           color: white;
           border: none;
           padding: 5px 10px;
           border-radius: 4px;
           cursor: pointer;
       }
       .copy-button:hover {
           background: #0056b3;
       }
   </style>
</head>
<body>
   <div class="code-container">
       <pre><code id="code">
$int = 42;          // integer
$float = 3.14;      // float
$string = "Hello";  // string
$bool = true;       // boolean
$array = [1, 2, 3]; // array
$object = new stdClass(); // object
$null = null;       // null
$resource = fopen("file.txt", "r"); // resource
$callable = function() { return "Hello"; }; // callable
       </code></pre>
       <button class="copy-button" onclick="copyCode()">复制</button>
   </div>
   <script>
       function copyCode() {
           const code = document.getElementById('code').innerText;
           navigator.clipboard.writeText(code).then(() => {
               alert('代码已复制到剪贴板!');
           }).catch(err => {
               console.error('复制失败: ', err);
           });
       }
   </script>
</body>
</html>

 

运行 HTML

实现步骤:

HTML:创建一个包含代码和复制按钮的容器。

CSS:设置代码框的样式,并将复制按钮定位在右上角。

JavaScript:使用 navigator.clipboard.writeText 方法将代码复制到剪贴板。

注意事项:
navigator.clipboard.writeText 是现代浏览器支持的 API,如果需要兼容旧版浏览器,可以使用 document.execCommand('copy') 作为备用方案。

如果代码内容较多,可以添加滚动条或限制高度。

通过以上代码,您可以在代码框右上角实现一个简单的复制功能。

标签

  • HTML
  • 登录或注册以发表评论

评论

相关文章

HTML空格符简介
打字游戏
html 中img元素的title属性作用
html 定位属性position 说明
html 块级元素 display:block详解
html中“input”如何设置位置?
html 实战制作页脚的实例
drupal 8 视图HTML标记文本元素说明

文章标签

CSS
Excel
ubuntu
php
五笔
vim
Linux
HTML
command
drupal 7
composer
centos
Mysql
drupal 8
模块(module)
VPN
drush
JavaSrcipt
apache
drupal 9
英语
Drupal
SEO
nerdtree
network
果树种植
surround
drupal10
主题(theme)
phpmyadmin
ssh
权限
yum
二进制
声音
ssl
selinux
HTML5
用户
password
Dreamweaver
flameshot
颜色
翻译
uploadprogress
DDEV
视图(views)
firewalld
gzip
快捷键
kernel
xdebug
错误
ckeditor
mbstring
C语言
源地址
简介
php扩展
电脑基础
certbot
git
apt
压缩解压
表单
电子秤
httpd
WireGuard
邮件验证
breadcrumb
AI
superfish
diff
重定向
网站备份
摄影
tagclouds
docker
java
csharp
短信验证
adsense
drupal9
电脑技巧
simple_adsense
算法
RSS
youtube
版本号
JavaScript
badblocks
端口
cookie
VUE
网站运营
计算机基础
gimp
form
mod_expires 模块
nginx
zip
http验证
重定向,301
curl
netstat
input
赚钱
ip
dns
RFID
标签优化
drupal11
maven
drupal 11
npm
哈希值
字段
更多
RSS源
本站由阿里云强力驱动,幸运券优惠链接
Copyright © 2019 - 2021 你行网 版权所有  粤ICP备19072650号