javascript - 如何遍历所有 Bootstrap 选项卡并在单击按钮时打印内容

标签 javascript jquery twitter-bootstrap

如何通过单击任何选项卡中的 Print 按钮来迭代所有 Bootstrap 选项卡并打印内容(不仅是事件标记的内容,还包括非事件标记的内容)。 每个选项卡都动态插入内容。尝试了我在相关 SO 解决方案中找到的一些建议的 css 更改。这些对我不起作用。

onclick 我的打印按钮的函数在这里

function printMe() {
  var theWork = window.open('', 'PrintWindow');
  var tabs_html = "<html><head><title>Print</title>";
  tabs_html += "<style>body { padding: 15px; }</style></head>";
  $(".tabs").each(function() {
    $(this).find(".nav-tabs li").each(function(index, element) {
      tabs_html += "<h2>" + $(this).text() + "</h2><br/>";
      tabs_html += $(".tab-content .tab-pane").eq(index).html() + "<br/><br/>";
    });
    $(this).after(tabs_html + "</body></html>");
  });
    theWork.document.open();
    theWork.document.write(tabs_html);
    theWork.document.close();
    theWork.print();
}

<强> Fiddle

最佳答案

您的代码有两个问题。

  1. 您将在 each 循环内附加结束 bodyhtml 标记。
  2. 您正在选择 tabs 类的元素。但提供的 DOM/Fiddle 中似乎没有这样的元素。

我已通过 1. 将结束 bodyhtml 标记移出循环,以及 2. 替换 .class< 解决了上述两个问题 选择器和 #id 选择器,瞧!它有效。

DEMO

关于javascript - 如何遍历所有 Bootstrap 选项卡并在单击按钮时打印内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38303871/

相关文章:

javascript - Backbone.js 应用程序的顶层应该是什么?一个 View ,通用对象,?

javascript - Web 套接字不适用于 JavaScript 中的 SSL 和 WSS

jquery - 使用 jQuery .stop() 进行无限循环

javascript - 遇到 JavaScript 对象作用域和 setTimeout 问题吗?

html - 如何在 Bootstrap 中添加固定宽度后使 div 响应

javascript - 使用 React 添加类

javascript - JS 异步库中的回调参数有什么作用?

javascript - DataTables 选定的行数据属性

jquery - Bootstrap 垂直滚动选项卡

html - 如何在填充的 div 旁边垂直对齐 div?