This is my code

<script>
var body = "dddddd"    
var script = "<script>window.print();</scr'+'ipt>";

var newWin = $("#printf")[0].contentWindow.document; 
newWin.open();
newWin.close();

$("body",newWin).append(body+script);

</script>
<iframe id="printf"></iframe>

This works but it prints the parent page, how do I get it to print just the iframe?

推荐答案

I would not expect that to work

试试看吧

window.frames["printf"].focus();
window.frames["printf"].print();

并使用

<iframe id="printf" name="printf"></iframe>

或者试试好的旧的

var newWin = window.frames["printf"];
newWin.document.write('<body onload="window.print()">dddd</body>');
newWin.document.close();

if jQuery cannot hack it

Live Demo

Jquery相关问答推荐

这是否会导致内存泄漏?

JQuery日期 Select 器未设置从jQuery中 Select 的月份起90天或3个月

如何用each替换元素的attr href

通过 jQuery 提取 application/json 数据

jQuery:获取数据属性

了解 Backbone.js REST 调用

使用 JQuery 更改 :before css Select 器的宽度属性

JSON 到字符串变量转储

jquery beforeunload 关闭(不离开)页面时?

使用 jquery/ajax 刷新/重新加载 Div 中的内容

jQuery.extend 和 jQuery.fn.extend 的区别?

offsetTop 与 jQuery.offset().top

将每 3 个 div 包装在一个 div 中

jQuery - 多个 :not Select 器

我想了解 jQuery 插件语法

虽然未定义变量 - 等待

如何使用 JQuery 获取 GET 和 POST 变量?

页面加载后如何执行jquery代码?

由于滚动,响应表内的 bootstrap 按钮下拉菜单不可见

Jquery Select 器输入[type=text]')