跳转到主要内容
你行网

Main navigation

  • 首页
  • Drupal
  • 编程与开发
  • 数据库
  • 操作系统与应用
  • 服务器与运维
  • 社区
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

评论

文章标签

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

相关文章

  • 因权限问题导致drupal cms 无法正常安装,那么该如何解决这个问题
  • kvm虚拟机启动失败:default网络未激活
  • ubuntu 安装kvm 虚拟机
  • 匿名函数(Anonymous Functions)详解
  • foreach 是 PHP 循环语句使用方法
  • implode() 函数的定义和用法
  • Drupal 11 主题制作教程
  • 检查 Composer 是否为官方源
  • 在 Drupal 中权重(Weight)是怎样排序的
  • 解决 Drupal 主题缺失问题
RSS源

友情链接 网站地图 版权信息 联系我们 网址导航 社区

Copyright © 2019 - 2021 你行网 版权所有  粤ICP备19072650号