我希望用户能够打印带有或不带有 aa div 中包含的某些信息的文档。我有 2 个样式表,其中一个指定了打印时要隐藏的 div 类的样式: .hide { display: none; }
以下是根据传递的参数选择样式表的脚本:
function print(o) {
var head = document.getElementsByTagName('head')[0].innerHTML;
if (o == 'withinfo') {
head += '<link rel="stylesheet" href="printwithinfo.css" media="print" />';
}
if (o == 'withoutinfo') {
head += '<link rel="stylesheet" href="printwithoutinfo.css" media="print" />';
}
document.getElementsByTagName('head')[0].innerHTML = head;
}
然后在我的html中,我有以下内容:
<div class="hide">My Info</div>
我的两个按钮如下:
<input type="button" value="Print with info" onclick="print('withinfo');">
<input type="button" value="Print without info" onclick="print('withoutinfo');">
不幸的是,当我单击任何一个按钮时,什么都没有发生。你能告诉我我做错了什么吗?