JavaScript @media打印样式表不起作用

时间:2013-08-28 20:47:46

标签: javascript html css printing

我已经开始尝试制作简单的HTML和CSS网页,同时加入一些JavaScript。我正在尝试制作一个简单的打印按钮,在打印时不会显示。我在SA周围搜索了各种答案,我看到很多关于link media= "print"的事情。在样式表中,您可以编写display: nonevisibility: hidden的类。然后,您可以将其应用于按钮。

问题在于,当我尝试这样做时,弹出页面预览时它不会变为不可见。这是我的主要代码:

<link rel="stylesheet" href="print.css"
type="text/css" media="print" />

<script type = "text/javascript">

function newPerson(firstname, lastname, age, gender, birthmonth, birthdate) {
this.firstname = firstname;
this.lastname = lastname;
this.age = age;
this.gender = gender;
this.birthmonth = birthmonth;
this.birthdate = birthdate;
this.birthyear = birthdayCalculate;
}
function birthdayCalculate() {
var date = new Date();
var CurYear = date.getFullYear()
var CurMonth = date.getMonth()
var birthyear = CurYear - this.age
if (this.birthmonth > CurMonth) {
    birthyear --
}
return birthyear
}

function testFunction(form) {
var firstName = form.firstName.value
var lastName = form.lastName.value
var Age = form.Age.value
var Gender = form.Gender.value
var birthMonth = form.birthMonth.value
var birthDate = form.birthDate.value

var new1 = new newPerson(firstName, lastName, Age, Gender, birthMonth, birthDate)
var person = new1.firstname + " " + new1.lastname + " is " + new1.age + " and is " +     new1.gender + " and was born on "
+ new1.birthmonth + "/" + new1.birthdate + "/" + new1.birthyear() + "." + "<br />"
document.write(person);

winVar = window.open();
winVar.document.write(person);
winVar.focus();
winVar.document.write(
"<input type='button' " +
"onClick= 'window.print();'" +
"value ='Print This Page' " +
"class = 'print' " +
"/>");}
</script>

我想你能告诉我用过的表格。我认为我不需要告诉你这件事。 print .css也非常简单:

.print{
visibility: hidden
}

有人看到我的脚本有什么问题吗?还有一点,我正在使用Google Chrome。

1 个答案:

答案 0 :(得分:1)

css在这种情况下无法使用javascript代替。你必须做这样的事情:

假设这是您的打印按钮     <input type="button" value="print" onclick="this.setAttribute('hidden''hidden')"/>

使用setAttribute函数单击按钮时隐藏按钮。

希望它能起作用..