»
Tiếng AnhTiếng PhápTiếng Việt

In - In nội dung này - JavaScriptBank.com

Phiên bản đầy đủ: jsB@nk » Ứng dụng » In nội dung này
URL: https://www.javascriptbank.com/smarter-print-this-page-script-print-this-content.html

In nội dung này © JavaScriptBank.comTên của hiệu ứng đã nói lên chức năng của nó - tạo một liên kết tại vị trí do bạn chỉ định (thông qua một id#); liên kết này có chức năng tạo một trang in cho một vùng nội dung cũng do bạn chỉ định, khi người dùng nhấn vào. Điểm đặc biệt của hiệu ứng là nếu trình duyệt của người dùng không hỗ trợ JavaScript thì liên kết này sẽ không hiển thị và nếu muốn in thì phải dùng lệnh in của trình duyệt.

Phiên bản đầy đủ: jsB@nk » Ứng dụng » In nội dung này
URL: https://www.javascriptbank.com/smarter-print-this-page-script-print-this-content.html



JavaScript
<script type="text/javascript"><!--// Created by Roger Johansson - http://www.456bereastreet.com/var addPrintLink = {init:function(sTargetEl,sLinkText) {if (!document.getElementById || !document.createTextNode) {return;} // Check for DOM supportif (!document.getElementById(sTargetEl)) {return;} // Check that the target element actually existsif (!window.print) {return;} // Check that the browser supports window.printvar oTarget = document.getElementById(sTargetEl);var oLink = document.createElement('a');oLink.id = 'print-link'; // Give the link an id to allow stylingoLink.href = '#'; // Make the link focusable for keyboard usersoLink.appendChild(document.createTextNode(sLinkText));oLink.onclick = function() {window.print(); return false;} // Return false prevents the browser from following the link and jumping to the top of the page after printingoTarget.appendChild(oLink);},addEvent:function(obj, type, fn) {if (obj.addEventListener)obj.addEventListener(type, fn, false);else if (obj.attachEvent) {obj["e"+type+fn] = fn;obj[type+fn] = function() {obj["e"+type+fn](window.event);}obj.attachEvent("on"+type, obj[type+fn]);}}};addPrintLink.addEvent(window, 'load', function(){addPrintLink.init('articleDemo','Print this page');});//--></script>


HTML
<div id="articleDemo"><p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diamnonummy nibh euismod tincidunt ut laoreet dolore magna aliquam eratvolutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tationullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.</p><a href="#" id="print-link"></a></div>