JavaScript@media打印样式表不工作

JavaScript @media print stylesheet not working

本文关键字:工作 样式 打印 JavaScript@media      更新时间:2023-09-26

我已经开始尝试制作简单的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>

我想你可以看出我用过表格。我想我不需要给你看这个。css也非常简单:

.print{
visibility: hidden
}

有人看到我的剧本有什么不对吗?还有一点,我用的是谷歌浏览器。

css在这种情况下使用javascript将没有帮助。你必须这样做:

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

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

希望它能起作用。。