2012-02-21 47 views
0

我试着用下面的代码输出使用document.write的计算,只有IE给我的结果。其他浏览器只是让我回来的形式。我找不到原因? 我试图用document.write()只给IE输出

<html> 
<head> 
<script type="text/javascript"> 
function main() 
{ 
    var area=form1.height.value*form1.base.value/2; 
    this.document.write("<p>The area is "+area+"</p>"); 
    //return form1.area.value=area; 
} 
</script> 
</head> 
<body> 
<h2>Triangle Area</h2> 
<form method="get" name="form1" id="form1"> 
    <p>Height: 
    <input name="height" type="text"> 
    </p> 
    <p>Base: 
    <input name="base" type="text"> 
    </p> 
    <p> 
    <input type="submit" name="Submit" value="Calculate" onclick="main()"> 
    </p> 
</form> 

</body> 
</html> 

回答

0

你不应该使用document.write这样。整个文档树完成后,document.write将覆盖整个页面。它实际上只能用于页面渲染时的内联脚本。

即使如此,它不建议。使用jQuery,或者非常令人沮丧的体验,document.createElement(..)appendChild(document.createTextNode(..))