打印功能主要代码:
function doprint(){
//给要打印的区域赋值给body
document.body.innerHTML = "要打印区域的innerHTML";
//打印
print();
}
遇到的问题:
1:动态赋值无法打印出来
解决法案例子如下:
绑定对应标签数据:
function bindData(){
//绑定type=checkbox,type=radio
$("input[type='checkbox'],input[type='radio']").each(function(){
if($(this).attr('checked'))
$(this).attr('checked',true);
else
$(this).removeAttr('checked');
});
//绑定 type=text,checkbox,radio,select>option的值
$("input,select option").each(function(){
$(this).attr('value',$(this).val());
});
//select选中值
$("select option").each(function(){
if($(this).attr('selected'))
$(this).attr('selected',true);
else
$(this).removeAttr('selected');
});
}
2:不能给table 中tbody.innHTML 赋值问题
例如 :document.getElementById("tbody").innerHTML = ""
解决方法:
function setTBodyInnerHTML(tbody,innerHtml){
var div = document.createElement("div");
div.innerHTML = '<table><tbody id="xxx">'+innerHtml+'</tbody></table>';
tbody.parentNode.replaceChild(div.firstChild.firstChild,tbody);
}