Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> JavaScript

Có thể tạo nút in (print button) không cần JavaScript?

Có, dễ dàng tạo nút in và gắn hình ảnh cho nó.

Ví dụ

<!DOCTYPE html>
<html>
  <body>
    <a href="javascript:window.print()">
      <img src="https://www.tutorialspoint.com/computer_fundamentals/images/inkjet_printer.jpg" height="50" alt="print the page">
    </a>
    <p>Nhấp ảnh trên để in trang.</p>
  </body>
</html>

Giải thích: href="javascript:window.print()" chạy JS khi nhấp link. Bọc <img> trong <a> tạo nút in dạng ảnh.

Hiện đại hơn: Dùng <button onclick="window.print()"> (semantic hơn <a> với href javascript:).