html - 打印 HTML DIV 的内容

标签 html angularjs

<分区>

我想打印一个 html div 的内容,目前我正在使用这段代码。

<div id="content">
</div>

var printContents = document.getElementById("content").innerHTML;
var originalContents = document.body.innerHTML;

document.body.innerHTML = printContents;
window.print();
document.body.innerHTML = originalContents;

打印工作正常。但在那之后我的其他功能停止响应。我正在使用 angular.js。

有什么想法吗?

最佳答案

这是我找到的答案

HTML 和 JS 代码

 <div id="printable">
    Print this div
  </div>
  <button ng-click="printDiv('printableArea');">Print Div</button>


$scope.printDiv = function(divName) {
  var printContents = document.getElementById(divName).innerHTML;
  var popupWin = window.open('', '_blank', 'width=300,height=300');
  popupWin.document.open();
  popupWin.document.write('<html><head><link rel="stylesheet" type="text/css" href="style.css" /></head><body onload="window.print()">' + printContents + '</body></html>');
  popupWin.document.close();
} 

我从here找到的

关于html - 打印 HTML DIV 的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38711917/

相关文章:

javascript - 禁用按钮,直到达到一个数字

angularjs - angular 指令处理 http 请求

javascript - 为什么这段代码不能专注于所需的 div?

javascript - 使用 ng-change 更新 Angular 模型

javascript - AngularJS 嵌套指令被插入到它们假定的父元素之外

javascript - 如何将参数从指令方法传递到 Controller ?

javascript - 如何使用angularjs仅选中ng-repeat复选框中的一个复选框?

html - 如何在 html/css 中使用字体

html - div彼此相邻,内联 block 仍然导致没有 float 的换行

html - 使用 meta http-equiv 标签重定向时避免将页面添加到浏览器历史记录