我有一个简单的html代码,我使用了javascript。(如果可以的话jquery建议我)
<script>
var myWindow;
function openWin() {
myWindow = window.open("", "myWindow", "width=200,height=100");
myWindow.document.write(#tobePrint);
}
function closeWin() {
myWindow.close();
}
</script>
<!DOCTYPE html>
<html>
<body>
<div id="tobePrint">Print the content of this div content in new window.</div>
<button onclick="openWin()">Open "myWindow"</button>
<button onclick="closeWin()">Close "myWindow"</button>
</body>
</html>
我正在尝试的新窗口出现在新选项卡中,我需要新窗口出现在选项卡的侧面。 div 内的内容应显示在新选项卡中window.So我尝试 myWindow.document.write(#tobePrint); 访问 Id 并尝试打印,但它不起作用。
最佳答案
这是工作解决方案:
var myWindow;
function openWin() {
myWindow = window.open("myWindow","newwin")
var tobePrint = $('#tobePrint').text();
myWindow.document.write('<p>' + tobePrint+ '</p>');
}
function closeWin() {
myWindow.close();
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body>
<div id="tobePrint">Print the content of this div content in new window.</div>
<button onclick="openWin()">Open "myWindow"</button>
<button onclick="closeWin()">Close "myWindow"</button>
</body>
关于javascript - 如何使用javascript或jquery在新窗口中打印特定的<div>内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53498613/