weixin_33727510 2015-08-07 17:37 采纳率: 0%
浏览 30

用图像打印Div

I am working on an MVC C# app wherein the contents of a div are composed with an AJAX call that retrieves database fields and returned as the success message to JavaScript. The string (a trivial example):

"<div style='background:yellow'>asdf<img src='myfile.jpg'></div>"

is then printed with:

function printDivContents(contents) {
    var printContents = contents;
    var originalContents = document.body.innerHTML;
    document.body.innerHTML = "<div style='margin:10px'>" + printContents + "</div>";
    window.print();
    document.body.innerHTML = originalContents;
}

Everything prints perfectly in most cases, but there is an image in there that prints if and only if it has been loaded previously. I would like it to always print. I have tried adding a preloading script to the content string. I can only use pure JavaScript, not jQuery, and all CSS has to be inline. What are my options to get the image to load? Should I pause execution to wait for the DOM to load, assign the image as a hidden div background on the calling page, or...?

  • 写回答

2条回答 默认 最新

  • 叼花硬汉 2015-08-07 17:54
    关注

    On the page that makes the initial AJAX call, create a hidden div as such:

    <div style="display:none; background:url('myfile.jpg')"></div>
    

    If the file is dynamically changed, change the background of the div prior to printing.

    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大