Làm cách nào để sao chép biến vào clipboard JavaScript?

Để sao chép văn bản từ một

function triggerExample() {
  const element = document.querySelector('#example');
  element.select();
  element.setSelectionRange(0, 99999);
  document.execCommand('copy');
}
5 hoặc
function triggerExample() {
  const element = document.querySelector('#example');
  element.select();
  element.setSelectionRange(0, 99999);
  document.execCommand('copy');
}
6, bạn nên gọi các phương thức
function triggerExample() {
  const element = document.querySelector('#example');
  element.select();
  element.setSelectionRange(0, 99999);
  document.execCommand('copy');
}
7 và
function triggerExample() {
  const element = document.querySelector('#example');
  element.select();
  element.setSelectionRange(0, 99999);
  document.execCommand('copy');
}
8 của phần tử, theo sau là
function triggerExample() {
  const element = document.querySelector('#example');
  element.select();
  element.setSelectionRange(0, 99999);
  document.execCommand('copy');
}
9.
function triggerExample() {
  const element = document.querySelector('#example');
  element.select();
  element.setSelectionRange(0, 99999);
  document.execCommand('copy');
}
7 và
function triggerExample() {
  const element = document.querySelector('#example');
  element.select();
  element.setSelectionRange(0, 99999);
  document.execCommand('copy');
}
8 chọn văn bản trong đầu vào và
function triggerExample() {
  const element = document.querySelector('#example');
  element.select();
  element.setSelectionRange(0, 99999);
  document.execCommand('copy');
}
9 sao chép lựa chọn hiện tại vào khay nhớ tạm

Dưới đây là một ví dụ trực tiếp


Click to Copy

function triggerExample() {
  const element = document.querySelector('#example');
  element.select();
  element.setSelectionRange(0, 99999);
  document.execCommand('copy');
}

Sao chép từ trường không nhập liệu

Để sao chép từ trường không nhập liệu, bạn phải tạo một

function triggerExample() {
  const element = document.querySelector('#example');
  element.select();
  element.setSelectionRange(0, 99999);
  document.execCommand('copy');
}
6 hoặc
function triggerExample() {
  const element = document.querySelector('#example');
  element.select();
  element.setSelectionRange(0, 99999);
  document.execCommand('copy');
}
5 tạm thời và đặt nội dung của mục nhập tạm thời đó thành văn bản bạn muốn sao chép bằng cách sử dụng hàm
function triggerExample() {
  const element = document.querySelector('#example');
  element.select();
  element.setSelectionRange(0, 99999);
  document.execCommand('copy');
}
3. Bạn cần thêm đầu vào vào trang. Bạn có thể xóa nó khỏi trang sau khi thực hiện xong
function triggerExample() {
  const element = document.querySelector('#example');
  element.select();
  element.setSelectionRange(0, 99999);
  document.execCommand('copy');
}
9

Sao chép dữ liệu từ trình duyệt vào clipboard của bạn là một tính năng nhỏ nhưng khá mạnh mẽ. Không còn lựa chọn thủ công &

function triggerExample() {
  const element = document.querySelector('#example');
  element.select();
  element.setSelectionRange(0, 99999);
  document.execCommand('copy');
}
6 sẽ tiết kiệm một chút thời gian khi theo dõi các bài viết viết mã lớn hơn. Tôi vừa mới thêm ký hiệu clipboard nhỏ bên cạnh tất cả các đoạn trích của mình để thuận tiện cho người đọc. Đọc để tìm hiểu làm thế nào tôi đã làm nó

bản mẫu


Tạo tệp

function triggerExample() {
  const element = document.querySelector('#example');
  element.select();
  element.setSelectionRange(0, 99999);
  document.execCommand('copy');
}
7, cũng như tệp
function triggerExample() {
  const element = document.querySelector('#example');
  element.select();
  element.setSelectionRange(0, 99999);
  document.execCommand('copy');
}
8 trong thư mục bạn chọn. Điền vào chỉ mục - điền thông tin sau và bắt đầu

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Copy to clipboard</title>
  </head>
  <body>
    <h1>With textarea</h1>
    <section>
      <textarea
        placeholder="Write your content here and press 'Copy Text'"
        id="clipboard-area"
        cols="30"
        rows="2"
      ></textarea>
      <textarea
        placeholder="Paste your content here"
        cols="30"
        rows="2"
      ></textarea>
    </section>
    <button style="width: 260px" onclick="handleCopyTextFromArea()">
      Copy text
    </button>

    <h1>Without textarea</h1>
    <section style="display: flex">
      <p style="width: 260px; margin: 0">
        Lorem ipsum, dolor sit amet consectetur adipisicing elit.
      </p>
      <textarea
        placeholder="Paste your content here"
        cols="30"
        rows="2"
      ></textarea>
    </section>
    <button style="width: 260px" onclick="handleCopyTextFromParagraph()">
      Copy text
    </button>

    <script src="main.js"></script>
  </body>
</html>

Vào chế độ toàn màn hình Thoát chế độ toàn màn hình

Chúng ta sẽ có hai chức năng được gắn vào các nút -

function handleCopyTextFromArea() {
  const area = document.querySelector('#clipboard-area')
  area.select();
  document.execCommand('copy')
}
0 và

_______41 - hãy lấp đầy chúng bằng sức sống.

Làm cách nào để sao chép biến vào clipboard JavaScript?

Phương pháp 1. execCommand('bản sao')

Mặc dù chức năng của phương pháp này không được dùng nữa theo tài liệu MDN, nhưng nó vẫn hoạt động tốt với thẻ html

function handleCopyTextFromArea() {
  const area = document.querySelector('#clipboard-area')
  area.select();
  document.execCommand('copy')
}
2. Và ngay cả khi bạn không có sẵn như vậy, việc tạo nó - và điền giá trị của nó bằng nội dung văn bản mong muốn - được thực hiện rất nhanh chóng

Nếu bạn có sẵn một vùng văn bản, hãy thêm phần sau vào

function triggerExample() {
  const element = document.querySelector('#example');
  element.select();
  element.setSelectionRange(0, 99999);
  document.execCommand('copy');
}
7 của bạn

function handleCopyTextFromArea() {
  const area = document.querySelector('#clipboard-area')
  area.select();
  document.execCommand('copy')
}

Vào chế độ toàn màn hình Thoát chế độ toàn màn hình

Nếu bạn không có phần tử đã nói theo ý của mình, bạn chỉ có thể thêm phần tử đó trong thời gian sao chép diễn ra. Các bước dưới đây thường diễn ra nhanh chóng, người dùng sẽ không nhận thấy thẻ bổ sung được thêm và xóa lại

  1. Tạo phần tử
    function handleCopyTextFromArea() {
      const area = document.querySelector('#clipboard-area')
      area.select();
      document.execCommand('copy')
    }
    
    2 và thêm nó vào DOM
  2. Điền vào giá trị của nó bằng văn bản từ đoạn văn - hoặc bất kỳ phần tử nào khác
  3. Sử dụng phương pháp
    function handleCopyTextFromArea() {
      const area = document.querySelector('#clipboard-area')
      area.select();
      document.execCommand('copy')
    }
    
    5 ở trên để sao chép nội dung văn bản
  4. Xóa phần tử một lần nữa

function triggerExample() {
  const element = document.querySelector('#example');
  element.select();
  element.setSelectionRange(0, 99999);
  document.execCommand('copy');
}
0

Vào chế độ toàn màn hình Thoát chế độ toàn màn hình

Nếu bây giờ bạn thử và

function handleCopyTextFromArea() {
  const area = document.querySelector('#clipboard-area')
  area.select();
  document.execCommand('copy')
}
6 vào vùng văn bản bên phải, bạn sẽ thấy đầu vào được dán. Mặc dù cách tiếp cận này vẫn hoạt động rất tốt, nhưng các trình duyệt hiện đại đã triển khai các giao diện riêng của chúng để xử lý các tính năng của khay nhớ tạm. Chúng ta hãy xem điều đó tiếp theo

Phương pháp 2. API bảng nhớ tạm

API bảng tạm là một cách tiếp cận hiện đại, dựa trên lời hứa chỉ tập trung vào bảng tạm thay vì thực hiện nhiều việc cùng một lúc. Nó yêu cầu bạn cho phép và chỉ hoạt động trên https, làm cho nó an toàn hơn theo thiết kế. Theo Tôi có thể sử dụng không, các trình duyệt cũ hơn không hỗ trợ chức năng này, do đó, bạn cũng có thể cân nhắc sử dụng giải pháp thay thế

Hãy thay thế hàm

function handleCopyTextFromArea() {
  const area = document.querySelector('#clipboard-area')
  area.select();
  document.execCommand('copy')
}
1 ở trên bằng hàm sau

function triggerExample() {
  const element = document.querySelector('#example');
  element.select();
  element.setSelectionRange(0, 99999);
  document.execCommand('copy');
}
3

Vào chế độ toàn màn hình Thoát chế độ toàn màn hình

Và đó là về nó, thực sự. Bạn có thể muốn thay thế

function handleCopyTextFromArea() {
  const area = document.querySelector('#clipboard-area')
  area.select();
  document.execCommand('copy')
}
8 - gọi lại bằng một số loại phản hồi của người dùng gọn gàng hơn và kiểm tra sự cho phép của người dùng, vẫn - đạt được mục tiêu - văn bản có sẵn và sẵn sàng để phân tích cú pháp

CẬP NHẬT. Sao chép vào clipboard với chỉ thị Vue 3

Nếu bạn đang sử dụng Vue. js để xây dựng ứng dụng của mình, bạn có thể bọc chức năng trên bằng chỉ thị tùy chỉnh Vue. Sau đó, bạn đạt được chức năng tương tự bằng cách nhấp vào (hoặc tương tác theo bất kỳ cách nào khác với) thành phần có v-clip liên kết với nó

Làm cách nào để sao chép giá trị vào clipboard JavaScript?

Để sao chép văn bản bằng API Clipboard mới, bạn sẽ sử dụng phương thức writeText() không đồng bộ . Phương thức này chỉ chấp nhận một tham số - văn bản cần sao chép vào khay nhớ tạm của bạn. Đây có thể là một chuỗi, một biến giữ mẫu theo nghĩa đen và các chuỗi khác hoặc một biến được sử dụng để giữ một chuỗi.

Làm cách nào để sao chép một chuỗi vào khay nhớ tạm trong JavaScript?

Tạo một vùng văn bản và đặt nội dung của nó thành văn bản bạn muốn sao chép vào khay nhớ tạm
Nối vùng văn bản vào DOM
Chọn văn bản trong vùng văn bản
tài liệu cuộc gọi. execCommand("bản sao")
Xóa vùng văn bản khỏi dom

Làm cách nào để dán từ khay nhớ tạm trong JavaScript?

readText(). then((clipText) => (dán. innerText = clipText)); Chúng tôi sử dụng API khay nhớ tạm và gọi hàm readText.

Làm cách nào để sao chép nội dung của div trong JavaScript?

Các bước sao chép văn bản trong javascript1. Tạo một phần tử textarea2. Lấy văn bản từ div bằng cách sử dụng div. bên trong3. .