admin管理员组文章数量:1425938
let say i have a print button on couple of pages and whenever user click . it will pop up the content in a modal and can print from there.Any idea will be appreciated.
I have couple of page with a print button . when user click it need to pul that content in a modal and than print from that modal.
let say i have a print button on couple of pages and whenever user click . it will pop up the content in a modal and can print from there.Any idea will be appreciated.
I have couple of page with a print button . when user click it need to pul that content in a modal and than print from that modal.
Share Improve this question asked Jul 10, 2011 at 0:31 paulpaul 1,1249 gold badges27 silver badges45 bronze badges 3- possible duplicate of Print the contents of a DIV – Brock Adams Commented Jul 10, 2011 at 0:47
- See, also: stackoverflow./questions/2603465/… – Brock Adams Commented Jul 10, 2011 at 0:48
- @Brock Adams thanks! but my requirement is a bit different ...but that link is helpful ...I am gonna add my code ... – paul Commented Jul 10, 2011 at 2:00
1 Answer
Reset to default 10I can't write the code for you right now but I can put you on the right track..
You need to use jquery to get the contents you want to print (likely $('body').html();
) then create your modal div popup and add an iframe into the modal div. Then add to the iframes body (via $('iframe').document.body.append();
) the print content. Then, call print
on the iframe ($('iframe').window.print;
)
Let me know if this makes sense?
EDIT: Here is some example code working with what I believe you want. (make sure you include jquery before this javascript code)
<body>
<script>
$(document).ready(function(){
$('#printmodal').click(function(){
// variables
var contents = $('#printable').html();
var frame = $('#printframe')[0].contentWindow.document;
// show the modal div
$('#modal').css({'display':'block'});
// open the frame document and add the contents
frame.open();
frame.write(contents);
frame.close();
// print just the modal div
$('#printframe')[0].contentWindow.print();
});
});
</script>
<style>
#modal {
display: none;
width: 100px;
height: 100px;
margin: 0 auto;
position: relative;
top: 100px;
}
</style>
<!-- Printable div (or you can just use any element) -->
<div id="printable">
<p>This is</p>
<p>printable</p>
</div>
<!-- Print link -->
<a id="printmodal" href="#">Print Me</a>
<!-- Modal div (intially hidden) -->
<div id="modal">
<iframe id="printframe" />
</div>
</body>
jsfiddle: http://jsfiddle/tsdexter/ke2rqt97/
本文标签: how to create a print modal using javascriptjquery etcStack Overflow
版权声明:本文标题:how to create a print modal using javascript, jquery etc - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741204946a2357965.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论