javascript 打印 InnerHtml

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14214988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 21:01:37  来源:igfitidea点击:

Printing InnerHtml

javascriptdomdocument

提问by TheMonkeyMan

Can someone tell me why 'printArea' in Javascript is comming up as undefined.

有人能告诉我为什么 Javascript 中的“printArea”未定义。

<html>
<head>
    <script language="text/javascript">
    function printArea(areaName)
    {
        var printContents = document.getElementById(areaName).innerHTML;
        var originalContents = document.body.innerHTML;

        document.body.innerHTML = printContents;

        window.print();

        document.body.innerHTML = originalContents;
    }   
</script>
</head>
<body>
<div id="printThis">
  Hello I am a printable area
 </div>
 <input type="button" onclick="printArea('printThis');" Value="Print">
</body>
</html>

Here is the JS Fiddle http://jsfiddle.net/cFeNp/

这是 JS Fiddle http://jsfiddle.net/cFeNp/

回答by Barakat

replace language="text/javascript"with type="text/javascript"

替换language="text/javascript"type="text/javascript"

回答by ncn corp

You javascript declaration is not right. Must be:

您的 javascript 声明不正确。必须是:

<script type="text/javascript">

<script type="text/javascript">

回答by Aarif Qureshi

If u want to get HTML of printThis Div,Here is the java script code for the same

如果你想获得 printThis Div 的 HTML,这里是相同的 java 脚本代码

<script type="text/javascript">
function printArea(areaName)
{
   var html =  document.getElementById('printThis').innerHTML; alert(html);
} 
</script>