如何遍历所有引导选项卡并在单击按钮上打印内容

时间:2016-07-11 09:48:13

标签: javascript jquery twitter-bootstrap

如何在单击任何选项卡中的Print按钮时,遍历所有引导选项卡并打印内容(不仅是活动标签的内容,还有非活动标签)。 每个标签都有动态插入的内容 。尝试了我在相关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 个答案:

答案 0 :(得分:0)

您的代码存在两个问题。

  1. 您要在body循环中追加结束htmleach标记。
  2. 您正在选择类tabs的元素。但是在提供的DOM / Fiddle中似乎没有这样的元素。
  3. 我已修复上述两个问题1.将结束bodyhtml标记移出循环,然后2.用{{1}替换.class选择器选择器,瞧!它有效。

    DEMO