Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用jQuery或JavaScript打印更改的div_Javascript_Jquery_Printing - Fatal编程技术网

如何使用jQuery或JavaScript打印更改的div

如何使用jQuery或JavaScript打印更改的div,javascript,jquery,printing,Javascript,Jquery,Printing,我有一个div,我可以通过改变它的十六进制值来控制它的颜色,但是我如何将它打印为我选择的任何颜色,而不是div上的原始颜色 例如:我的div为白色。我将颜色更改为红色,我有一个按钮,可以打印div,但它显示我打印的是白色div,而不是红色 如果我理解了您的问题,您可以在printspessional()的顶部添加以下代码行: $('#printReady').css('background', '#ff0000'); 不过,控制这类事情的更好方法是使用样式 <style> .

我有一个
div
,我可以通过改变它的十六进制值来控制它的颜色,但是我如何将它打印为我选择的任何颜色,而不是
div
上的原始颜色

例如:我的
div
为白色。我将颜色更改为红色,我有一个按钮,可以打印
div
,但它显示我打印的是白色
div
,而不是红色


如果我理解了您的问题,您可以在printspessional()的顶部添加以下代码行:

$('#printReady').css('background', '#ff0000');
不过,控制这类事情的更好方法是使用样式

<style>
   .ready {
     background: #ffffff;
   }

   .printing {
     background: #ff0000;
   }
</style>
当您想将其更改回原样时,请按相反的顺序添加样式。

html += '<style>* { color: red }</style>';
html+='*{color:red}';
比如说

<script language="JavaScript">
var gAutoPrint = true; // Tells whether to automatically call the print function

function printSpecial() {
  if (document.getElementById == null) return false;
  var printReadyElem = document.getElementById("printReady");
  if (printReadyElem==null) {
    alert("Could not find the printReady function");
    return false;
  }

  var html = '<HTML>\n<HEAD>\n';

  if (document.getElementsByTagName != null) {
    var headTags = document.getElementsByTagName("head");
    if (headTags.length > 0) html += headTags[0].innerHTML;
  }

  html += '<style>* { color: red }</style>';
  html += '\n</HEAD>\n<BODY>\n';
  html += printReadyElem.innerHTML;
  html += '\n</BODY>\n</HTML>';

  var printWin = window.open("","printSpecial");
  if (printWin) {
    printWin.document.write(html);
    printWin.document.close();
    if (gAutoPrint) {
      printWin.focus().print();
    }
  }    
  else {
    alert("The print ready feature is only available if you are using a browser that supports it and you have not blocked popups. Please update your browswer.");
  }
  return false;
}
</script>


this is my button and selected div

  <div id="printReady"> 

  div i want to print
  </div>

  <a href="#" onclick="return printSpecial()" style="position:absolute">Print this Page</a>

var gAutoPrint=true;//指示是否自动调用打印函数
函数printSpecial(){
if(document.getElementById==null)返回false;
var printReadyElem=document.getElementById(“printReady”);
if(printReadyElem==null){
警报(“找不到printReady函数”);
返回false;
}
var html='\n\n';
if(document.getElementsByTagName!=null){
var headTags=document.getElementsByTagName(“head”);
如果(headTags.length>0)html+=headTags[0].innerHTML;
}
html+='*{color:red}';
html+='\n\n';
html+=printReadyElem.innerHTML;
html+='\n\n';
var printWin=window.open(“,“printSpecial”);
if(printWin){
printWin.document.write(html);
printWin.document.close();
如果(gAutoPrint){
printWin.focus().print();
}
}    
否则{
警报(“打印就绪功能仅在您使用支持它的浏览器且未阻止弹出窗口时可用。请更新您的浏览器。”);
}
返回false;
}
这是我的按钮和选择的div
我要打印的div
不客气。请看
$('#printReady').removeClass('ready');
$('#printReady').addClass('printing');
html += '<style>* { color: red }</style>';
<script language="JavaScript">
var gAutoPrint = true; // Tells whether to automatically call the print function

function printSpecial() {
  if (document.getElementById == null) return false;
  var printReadyElem = document.getElementById("printReady");
  if (printReadyElem==null) {
    alert("Could not find the printReady function");
    return false;
  }

  var html = '<HTML>\n<HEAD>\n';

  if (document.getElementsByTagName != null) {
    var headTags = document.getElementsByTagName("head");
    if (headTags.length > 0) html += headTags[0].innerHTML;
  }

  html += '<style>* { color: red }</style>';
  html += '\n</HEAD>\n<BODY>\n';
  html += printReadyElem.innerHTML;
  html += '\n</BODY>\n</HTML>';

  var printWin = window.open("","printSpecial");
  if (printWin) {
    printWin.document.write(html);
    printWin.document.close();
    if (gAutoPrint) {
      printWin.focus().print();
    }
  }    
  else {
    alert("The print ready feature is only available if you are using a browser that supports it and you have not blocked popups. Please update your browswer.");
  }
  return false;
}
</script>


this is my button and selected div

  <div id="printReady"> 

  div i want to print
  </div>

  <a href="#" onclick="return printSpecial()" style="position:absolute">Print this Page</a>