我为了以下目的创建了一个greasemonkey脚本:
按钮显示,但它不会触发我的功能,此时只打印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();
答案 0 :(得分:2)
这一行有一个错字:
var divToPrint=document.getEelementById("loanTable"); \
将其更改为:
var divToPrint=document.getElementById("loanTable"); \
或者,将此行添加到脚本的metadata section:
// @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();
} );