简体   繁体   English

我们可以动态使用document.getElementById()吗?

[英]Can we use document.getElementById() dynamically

I am trying to print an html page using window.print. 我正在尝试使用window.print打印html页面。 Where as the html page is getting updated for each rest API call. 在html页面上为每个其余API调用更新的位置。 so can i print continuously for each rest API call??? 所以我可以为每个其余的API调用连续打印吗???

    var myPrintContent = document.getElementById("data-box"); 
    var myPrintWindow = window.open('','Print-Window');
    var popupWindow = window.open('','Print-Window');
    popupWindow.document.write('<TITLE></TITLE>\n');
    popupWindow.document.write('<URL></URL>\n');
    popupWindow.document.write('<script>\n');
    popupWindow.document.write('function print_win(){\n');
    popupWindow.document.write('\nwindow.print();\n');     
    popupWindow.document.write('}\n');
    popupWindow.document.write('<\/script>\n');
    popupWindow.document.write('</HEAD>\n');
    popupWindow.document.write('<BODY onload="print_win()" >\n');         
    popupWindow.document.write(myPrintContent.innerHTML)
    popupWindow.document.write('</BODY>\n');
    popupWindow.document.write('</HTML>\n');
    popupWindow.document.close();

This is what i am doing. 这就是我在做什么。 so content of data-box.innerhtml is different for different employees. 因此data-box.innerhtml的内容对于不同的员工而言是不同的。 So on each time i call print one page i will get. 因此,每次我调用print时,我都会得到一页。 But for the next employee if i am again calling the print function the called employee details is only getting printed. 但是对于下一个员工,如果我再次调用打印功能,那么被调用的员工详细信息只会被打印出来。 So what i want is if i gave different id s for different employees, how can i get all the ids dynamically. 所以我想要的是,如果我为不同的员工提供了不同的ID,那么我该如何动态获取所有ID。 so that once i call print i can get all the data s of all employee printed. 这样,一旦我打电话给print,我就可以打印所有员工的所有数据。

window.print();

as can be read at w3school is a method to print the current window. 可以在w3school上阅读,这是一种打印当前窗口的方法。 It has nothing to with document.getElementById() method. 与document.getElementById()方法无关。 If your are interested in printing your page, simple print the window. 如果您有兴趣打印页面,则只需打印窗口即可。 That's it. 而已。

But if you want to use document.getElementById() for some other purposes, you can pass variable (the dynamic id) of element in consideration. 但是,如果您想将document.getElementById()用于其他目的,则可以考虑使用元素的变量(动态ID)。 here is how you do it 这是你的做法

var myId = "my-element-id";
var myContent = document.getElementById(myId).innerHTML;

Better post any code for better help. 更好地发布任何代码以获得更好的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM