简体   繁体   English

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

[英]JavaScript @media print stylesheet not working

I have started trying to make simple HTML and CSS webpages while incorporating a bit of JavaScript. 我已经开始尝试制作一些简单的HTML和CSS网页,同时引入一些JavaScript。 I'm trying to make a simple print button that doesn't show up when you print it. 我正在尝试制作一个简单的打印按钮,当您打印该按钮时不会显示它。 I've searched around SA for various answers and I've seen a lot of things about the link media= "print" . 我在SA周围搜索了各种答案,并且看到了许多有关link media= "print" In the stylesheet would be a class where you would write either display: none or visibility: hidden . 在样式表中将是一个类,您可以在其中编写display: nonevisibility: hidden You would then apply that to your button. 然后,您可以将其应用于按钮。

The problem is that when I try doing it, it doesn't turn invisible when the page preview pops up. 问题是,当我尝试执行此操作时,弹出页面预览时它不会变得不可见。 Here is my main code: 这是我的主要代码:

<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>

I think you'd be able to tell I used forms. 我认为您可以告诉我使用的表格。 I don't think I need to show you this. 我认为不需要向您展示。 The print .css is extremely simple too: 打印.css也非常简单:

.print{
visibility: hidden
}

Does anyone see anything wrong with my script? 有人看到我的脚本有什么问题吗? A little something else, I'm using Google Chrome. 还有一点,我正在使用Google Chrome。

css will not help in this case use javascript instead. 在这种情况下,css将无济于事,而是使用javascript。 You have to do something like this: 您必须执行以下操作:

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

hide the button when it clicked using the setAttribute function. 使用setAttribute函数单击按钮时隐藏按钮。

hope it will work.. 希望它能工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM