1

我创建了一个greasemonkey脚本,目的是:

  • 将打印收据按钮添加到 3rd 方 Web 应用程序页面
  • 在新窗口中将收据打印机友好样式添加到 div
  • 打印此窗口

按钮出现了,但它不会触发我的函数,此时它只是打印 div。

这是我所在的位置:

var scriptElement = document.createElement('script');
scriptElement.type = 'text/javascript';

scriptElement.innerHTML =  'function printReceipt() { \
var divToPrint=document.getEelementById("loanTable"); \
newWin= window.open(""); \
newWin.document.write(divToPrint.outerHTML); \
newWin.print(); \
newWin.close(); \
}';

document.getElementsByTagName("head")[0].appendChild(scriptElement);

window.addButton = function () {
    // Get the location on the page where you want to create the button
    var targetDiv = document.getElementById('newcheckout');

    // Create a div to surround the button
    var newDiv = document.createElement('div');
    newDiv.setAttribute('id', 'autoCheckOrder');

    // Create the button and set its attributes
    var inputButton = document.createElement('input');
    inputButton.name = 'autoCheckOrderButton';
    inputButton.type = 'button';
    inputButton.value = 'Print Receipt?';
    inputButton.setAttribute("onclick", "printReceipt();");

    // Append the button to the div
    newDiv.appendChild(inputButton); 
    targetDiv.appendChild(newDiv);
}
addButton();
4

1 回答 1

3

这一行有一个错字:

var divToPrint=document.getEelementById("loanTable"); \

将其更改为:

var divToPrint=document.getElementById("loanTable"); \



或者,将此行添加到脚本的元数据部分

// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js

那么整个脚本就变成了:

$("#newcheckout").append ('<div id="autoCheckOrder"></div>');
$("#autoCheckOrder").append ('<button>Print Receipt?</button>');

$("#autoCheckOrder button").click ( function () {
    var divToPrint  = document.getElementById ("loanTable"); 
    var newWin      = window.open (""); 
    newWin.document.write (divToPrint.outerHTML); 
    newWin.print(); 
    newWin.close(); 
} );
于 2012-05-03T04:05:51.253 回答