简体   繁体   English

如何使window.print()在本地运行?

[英]How to get window.print() to run locallly?

Hi I have been writing a website for the last couple of weeks but cannot get the Javascript function window.print() to run locally in my browser. 嗨,我已经写了一个网站过去两周,但是无法在浏览器中本地运行Javascript函数window.print()。 I do not have server space yet so I cannot check the online functionality. 我没有服务器空间,因此无法检查在线功能。 Here is my code: 这是我的代码:

    <form>
        <input type="button" value="Print this page" onclick="window.print();" />
    </form> 

Does anyone know if this function can run on a local machine? 有人知道此功能是否可以在本地计算机上运行吗? Or is there a problem with my code? 还是我的代码有问题? Thanks 谢谢

The syntax is fine. 语法很好。 It should work. 它应该工作。

You might also want to try onclick="javascript:window.print();" 您可能还想尝试onclick =“ javascript:window.print();” and see if that works. 看看是否可行。

Another option you can try then is to create a function inside a script tag and call it from your onclick event: 您可以尝试的另一种选择是在script标签内创建一个函数,并从onclick事件中调用它:

<input type="button" value="Print this page" onclick="PrintMe()" />

<script>
    function PrintMe(){
        window.print();
    }
</script>

It might be a problem with the form tag.. 表单标签可能有问题。

try using button without form requirement 尝试使用没有表单要求的按钮

<button onclick="window.print();">Print this page</button>

Or if you are using the form 或者如果您使用的是表格

<form>
    <input type="button" value="Print this page" onclick="window.print();return false;" />
</form> 

if alert works and then enables this to work what happens if you try 如果警报起作用,然后使它起作用,如果尝试,会发生什么

onclick="console.log(window.print());return false;"

Capitilize the C in onClick. 在onClick中将C首字母大写化。 That might help. 这可能会有所帮助。 That's how I learned it, anyway. 无论如何,这就是我的学习方法。

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

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