打印InnerHtml

时间:2013-01-08 12:29:46

标签: javascript domdocument

有人能告诉我为什么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>

这是JS小提琴http://jsfiddle.net/cFeNp/

3 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:1)

你javascript声明不对。必须是:

  

<script type="text/javascript">

答案 2 :(得分:0)

如果你想获得printThis Div的HTML,这是同一个

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