1

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

有人看到我的脚本有什么问题吗?有点别的,我正在使用谷歌浏览器。

4

1 回答 1

1

在这种情况下,css 将无济于事,而是使用 javascript。你必须做这样的事情:

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

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

希望它会工作..

于 2013-08-30T06:54:47.973 回答