0

16 進数値をdiv変更することで色を制御できる がありますが、元の色の代わりに選択した色で印刷するにはどうすればよいdivですか?

: 私divです。色をに変更し、それを印刷できるボタンがありますが、ではなくdivを印刷していることがわかります。 div

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

function printSpecial()
{
if (document.getElementById != null)
{
var html = '<HTML>\n<HEAD>\n';

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

html += '\n</HE>\n<BODY>\n';

var printReadyElem = document.getElementById("printReady");

if (printReadyElem != null)
{
html += printReadyElem.innerHTML;
}
else
{
alert("Could not find the printReady function");
return;
}

html += '\n</BO>\n</HT>';

var printWin = window.open("","printSpecial");
printWin.document.open();
printWin.document.write(html);
printWin.document.close();
if (gAutoPrint)
printWin.print();
}
else
{
alert("The print ready feature is only available if you are using a browser. Please
update your browser.");
}
}

</script>

これは私のボタンであり、選択されていますdiv:

<div id="printReady"> 

div I want to print
</div>

<a href="javascript:void(printSpecial())" style="position:absolute">Print this Page</a>
4

2 に答える 2

0

あなたの質問を理解できたら、printSpecial() の先頭に次のコード行を追加してください。

$('#printReady').css('background', '#ff0000');

ただし、この種のことを制御するより良い方法は、スタイルを使用することです。

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

   .printing {
     background: #ff0000;
   }
</style>

次に、jQuery.addClass と jQuery.removeClass を使用できます。そうすれば、CSS を両方の「状態」に合わせたいものに変更するだけです。

次に、これを実行してスタイルを変更します。

$('#printReady').removeClass('ready');
$('#printReady').addClass('printing');

そして、元に戻したいときは逆にします。

于 2012-10-15T09:22:37.607 に答える
0

スタイルを追加

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>
于 2012-10-15T09:24:32.220 に答える