Hongmu Notes
Home Language Notes jquery click to copy specified text
Language Notes JavaScript

jquery click to copy specified text

jquery click to copy specified text

A wave of realization for everyone from childhood to adulthood!

Click copy without prompting

// 给复制按钮绑定点击事件
$('#copyBtn').on('click', function() {
  // 选择要复制的文本
  var text = $('#inputBox').val();

  // 创建一个临时的textarea元素,并将文本设置为它的值
  var $temp = $('<textarea>');
  $('body').append($temp);
  $temp.val(text).select();

  // 复制文本到剪贴板
  document.execCommand('copy');

  // 移除临时元素
  $temp.remove();
});

Click copy and a pop-up window will appear.

// 给复制按钮绑定点击事件
$('#copyBtn').on('click', function() {
  // 选择要复制的文本
  var text = $('#inputBox').val();

  // 创建一个临时的textarea元素,并将文本设置为它的值
  var $temp = $('<textarea>'); // 创建一个临时的textarea元素
  $('body').append($temp); // 将临时元素添加到页面中
  $temp.val(text).select(); // 将文本设置为临时元素的值,并选中文本

  // 复制文本到剪贴板
  document.execCommand('copy');

  // 移除临时元素
  $temp.remove();

  // 弹出提示框
  alert('已复制到剪贴板');
});

Click to copy the boostrap pop-up prompt

<!-- 引入 Bootstrap 样式文件和 JavaScript 文件 -->
<link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/bootstrap/4.6.0/css/bootstrap.min.css">
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/bootstrap/4.6.0/js/bootstrap.min.js"></script>

<!-- 在页面中添加一个 Bootstrap 弹窗提示的容器 -->
<div class="modal fade" id="copyModal" tabindex="-1" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">提示</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="关闭">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        已复制到剪贴板
      </div>
    </div>
  </div>
</div>

<!-- 在 JavaScript 中绑定复制按钮的点击事件 -->
<script>
  $('#copyBtn').on('click', function() {
    var text = $('#inputBox').val();
    var $temp = $('<textarea>');
    $('body').append($temp);
    $temp.val(text).select();
    document.execCommand('copy');
    $temp.remove();
    $('#copyModal').modal('show'); // 显示 Bootstrap 弹窗提示
  });
</script>

final effect

ad1a1b7183204816

 

微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

js move the mouse up to modify the style attribute

js move the mouse up to modify the style attribute Language Notes JavaScript

When the mouse hovers over the element, the color changes to red; when the mouse leaves the element, the color changes to black. To retrieve an element by its ID: `document.getElementById()`; Event handler for mouse hover: `onmouseover`; Event handler for mouse leave: `onmouseout`.<!DOCTYPE html><html lang= "en">...
👁 130

Recommended reading

(Adaptive mobile version) PBootCMS template for high-end luxury watch websites – Download responsive luxury watch website source code – 0419

(Adaptive mobile version) PBootCMS template for high-end luxury watch websites – Download responsive luxury watch website source code – 0419 Practical Collection pbootcms Template

This premium, high-end mobile-friendly watch collection and luxury watch brand website template based on PbootCMS responsive framework. The design features a sophisticated, luxurious aesthetic, ideal for showcasing luxury timepieces, brand stories, and exquisite craftsmanship. It enables luxury brands to effectively communicate their brand value online and attract high-net-worth customers. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn
👁 56
EmpireCMS – Article Download, Images & Videos, E-commerce Store, Taobao Affiliate – Full-Website Template (Adaptive HTML5 Responsive) 039

EmpireCMS – Article Download, Images & Videos, E-commerce Store, Taobao Affiliate – Full-Website Template (Adaptive HTML5 Responsive) 039 Practical Collection empire template

The latest EmpireCMS core solution is designed for displaying news articles, showcasing images, and enabling file resource downloads (download limits and user permissions can be configured via the backend); video playback (with adaptive mobile playback support; allows direct uploading and playback of external MP4 or M3U8 files, or direct use of sharing codes from platforms such as Youku Tudou – a transcoding plugin can be integrated to directly transcode and play videos from various platforms); product sales (includes "Purchase Within Site" functionality and a backend payment interface...).
👁 456
Responsive record memory simple blog website template 0364

Responsive record memory simple blog website template 0364 Practical Collection Yiyou template

An eyoucms responsive template for a simple blog website that records and remembers. The design style is warm and simple, suitable for personal bloggers to record life moments, share emotional stories and daily insights. Helps individuals build a private and warm content sharing space online. Template display Installation instructions Website backend: /login.php Account: admin Password: admin Related articles Summary of common problems in Yiyou CMS installation Yiyou C...
👁 42
(Adaptive Mobile Version) High-end, sophisticated technology-themed PBootCMS website template featuring three-level navigation, download and recruitment functions – 0420

(Adaptive Mobile Version) High-end, sophisticated technology-themed PBootCMS website template featuring three-level navigation, download and recruitment functions – 0420 Practical Collection pbootcms Template

An premium, sophisticated, and technologically advanced PbootCMS website template designed for mobile devices. Featuring a cutting-edge design and powerful functionality—including three-level navigation, download and recruitment features—this template is ideal for large technology companies and conglomerates to showcase their brand image, product and service offerings, as well as their talent recruitment initiatives. Template Preview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn | Related Articles: Pb...
👁 37
Responsive personal blog and self-media article-based website template – 0365

Responsive personal blog and self-media article-based website template – 0365 Practical Collection Yiyou template

This EyouCMS responsive template is ideal for personal blogs and self-media content-driven websites. Its clean, modern design makes it perfect for content creators to publish articles, share insights, and share operational experiences. It helps self-media creators build their online personal brand and attract readers. Template Preview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS...
👁 37
(Adaptive mobile version) Yellow responsive construction machinery equipment PBootCMS website template – Download HTML5 excavator website source code – 0421

(Adaptive mobile version) Yellow responsive construction machinery equipment PBootCMS website template – Download HTML5 excavator website source code – 0421 Practical Collection pbootcms Template

This adaptive, responsive PbootCMS website template for construction machinery equipment, designed for mobile devices, utilizes HTML5 technology. Its eye-catching and professional design makes it ideal for showcasing products such as excavators and engineering vehicles. It enables construction machinery companies to showcase their products online and attract clients in the construction and mining industries. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s...
👁 63