使用JS在for循环中打印

时间:2018-12-12 10:26:23

标签: javascript

我需要在javascript中打印带有for循环的div,我知道该问题可能与异步功能有关,但我无法解决该问题?有小费吗? (添加警报将允许我打印所有迭代的div)

function printmex(){
var qty=document.getElementById("qtr").value;

for(var i=0;i<qty;i++){
var rc="receipt"+i;

var divToPrint1=document.getElementById(rc);
newWin2= window.open("","newWin2","width=1,height=1");
newWin2.document.write(divToPrint1.outerHTML);
newWin2.print();
newWin2.close();
}
}

1 个答案:

答案 0 :(得分:0)

也许这段代码对您有帮助

function printmex(counter) {

  const win = window.open('', 'win', 'status');

  for (let i = 0; i < counter; i++) {
    const box = win.document.createElement('div');
    box.innerText = counter.toString();
    box.id = counter.toString();

    win.document.body.appendChild(box);
  }

  win.print();
}

printmex(5);