<html>
<head>
<script type="text/javascript">
function createNewDoc()
  {
  var newDoc=document.open("text/html","replace");
  var txt="<html><body>Learning about the DOM is FUN!</body></html>";
  newDoc.write(txt);
  newDoc.close();
  }
</script>
</head>
<body><input type="button" value="Write to a new document"
onclick="createNewDoc()"></body>
</html>
可以正确运行,可是把var newDoc=document.open("text/html","replace");newDoc.write(txt);放在外面却会抱错,有知道原因的吗?
<html>
<head>
<script type="text/javascript">  var newDoc=document.open("text/html","replace");
  var txt="<html><body>Learning about the DOM is FUN!</body></html>";
  newDoc.write(txt);
  newDoc.close();
  
</script>
</head>
<body><input type="button" value="Write to a new document"
onclick="createNewDoc()"></body>
</html>

解决方案 »

  1.   


    <html>
    <head>
    <script type="text/javascript">
    function createNewDoc()
      {
      var newDoc=document.open("text/html","replace");
      var txt=" <html> <body>Learning about the DOM is FUN! </body> </html>";
      newDoc.write(txt);
      newDoc.close();
      }
    </script>
    </head>
    <body><input type="button" value="Write to a new document"
    onclick="createNewDoc()"></body>
    </html> 
    没问题呀!为什么要把var newDoc=document.open("text/html","replace");newDoc.write(txt)放到函数外呢
      

  2.   

    错误是 newDoc为空或者不是对象 有知道原因的吗?
      

  3.   

    你是说这种情况么?<html>
    <head>
    <script type="text/javascript">var newDoc=document.open("text/html","replace");
    var txt=" <html> <body>Learning about the DOM is FUN! </body> </html>";
    newDoc.write(txt);
    newDoc.close();</script>
    </head>
    <body></body>
    </html>
      

  4.   

    我电脑上有的浏览的测试:
    Firefox 3.0.8:pass
    Safari 3.2.2:
    ---------------------------------------
    TypeError: Undefined value(line 7)opera 9.61:
    ----------------------------------------
    name: TypeError
    message: Statement on line 5: Cannot convert undefined or null to Object
    Backtrace:
      Line 5 of inline#1 script in http://localhost/test/19.html
        newDoc.write(txt);
    stacktrace: n/a; see 'opera:config#UserPrefs|Exceptions Have Stacktrace'IE 7.0
    ----------------------------------------
      Line 7,char 1,error:'newdoc'为空或不是对象
      

  5.   

    alert(newDoc)opera && safari:undefined
    ie:null
      

  6.   

    只有firefox 3提示:object HTMLDocument
      

  7.   

    //var newDoc=document.open("text/html","replace")
    //其中newDoc是对象(Element),返回对新的 document 对象的引用。
    <html> 
    <head> 
    <script type="text/javascript">   var newDoc=document.open("text/html","replace"); //运行到这里,对象获取不了,因为整个document还没有读取完整,返回的应
                                                       //该是newDoc==null。
      var txt=" <html> <body>Learning about the DOM is FUN! </body> </html>"; 
      newDoc.write(txt); 
      newDoc.close(); 
    </script> 
    </head> 
    <body> 
    </body> 
    </html>
      

  8.   

    html 页面在加载完成后才能创建documnet对象,你出错的代码写法是在页面没有加载完成的时候就创建document,对象为undefined或null就正常了