有一段打印弹出页代码,如不打印弹出页而改为打印当前页,此代码怎么改?
function cereateDiv(){
var url = '';
var div = document.createElement('div');
var cw = Math.max(document.body.clientWidth , document.documentElement.clientWidth);
var ch = Math.max(document.body.clientHeight , document.documentElement.clientHeight);
div.style.position = 'absolute';
div.style.width = '300px';
div.style.height = '200px';
div.style.border = '1px solid #000';
div.style.top = ch/2 - 100+'px';
div.style.left = cw/2 - 150+'px';
div.style.zIndex = 99;
document.body.appendChild(div);
var cover = div.cloneNode();
cover.style.width = cw + 'px';
cover.style.height = ch + 'px';
cover.style.border = 'none';
cover.style.background = '#ddd';
cover.style.top = '0px';
cover.style.left = '0px';
cover.style.position = 'absolute';
cover.style.zIndex = 98;
document.body.appendChild(cover);
cover.style.opacity = 0.8
cover.style.filter = 'alpha:(opacity=80)';
div.innerHTML = '<div class="info">确定打印?</div><div class="buttons"><input type="button" id="confirm" value="打印"/> <input type="button" id="cancel" value="取消打印"/></div>'
document.getElementById('confirm').onclick = function(){
var target = window.open(url );
target.print();//这里不知道能不能打印成功,如果新打开的页面是自己可以控制的话,建议打印代码放在这个页面里
document.body.removeChild(cover);
document.body.removeChild(div);
}
document.getElementById('cancel').onclick = function(){
document.body.removeChild(cover);
document.body.removeChild(div);
}
}
------解决方案--------------------
document.getElementById('confirm').onclick = function(){
document.body.removeChild(cover);
document.body.removeChild(div); //先去掉弹出框跟蒙版层
url=='' && window.open(url);//你不给url赋值一个弹出的新页面的URL的么??
window.print();//改这里。