日期:2014-05-17  浏览次数:20584 次

两个代码基本一样,在firefox里为什么一个不能实现topMargin=0?
两个页面,代码基本一样,但是test1.htm在firefox 3.6里不能实现topMargin=0,搞不懂,是firefox的特性?请高手赐教。
test1.htm,源码如下
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>这个没有实现topMargin=0</title>
</head>
<BODY leftMargin=0 background="" topMargin=0 >
<script  language=javascript>
document.write('<p>这个没有实现topMargin=0 </p>');
</script>
</body>
</html>

test2.htm,源码如下
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>'这个实现了topMargin=0</title>
</head>
<BODY leftMargin=0 background="" topMargin=0 >
<p>
<script  language=javascript>
document.write('这个实现了topMargin=0 ');
</script>
</p>
</body>
</html>
------解决方案--------------------
topMargin一直是0,前面那个p多了margin

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>这个没有实现topMargin=0</title>
<style type="text/css">
p{margin:0px;}
</style>
</head>
<BODY leftMargin=0 background="" topMargin=0 >
<script language=javascript>
document.write('<p>这个没有实现topMargin=0 </p>');
</script>
</body>
</html>

------解决方案--------------------
引用:
应该是同样的效果啊,一个是预先写了<p></p>,一个是用js输出<p></p>,但是用js输出的不能够紧挨浏览器顶端,这个是什么原因呢?

document.write时,会把原有的内容都擦除,也就是说原来的<P>已经不存在了,所以才靠顶的。
而在document.write里再加上<P>的话,它就产生作用了。

by theforever_csdn