1

有人可以告诉我为什么 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 Fiddle http://jsfiddle.net/cFeNp/

4

3 回答 3

4

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

于 2013-01-08T12:36:58.563 回答
1

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

<script type="text/javascript">
function printArea(areaName)
{
   var html =  document.getElementById('printThis').innerHTML; alert(html);
} 
</script>
于 2013-01-08T12:44:41.767 回答
1

你的javascript声明不正确。一定是:

<script type="text/javascript">

于 2013-01-08T12:38:50.477 回答