<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>
<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>
<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)放到函数外呢
<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>
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'为空或不是对象
ie:null
//其中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>