您的位置:首页 > 编程语言

符合标准的对联广告代码

2009-03-17 13:35 316 查看
原本工作正常的对联广告突然宣布罢工,为什么?标准化真的那么脆弱吗?"

不符合标准的正常工作的对联广告:

<html xmlns="http://www.w3.org/1999/xhtml">

2<head>

3<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />

4<title>不符合标准的正常工作的对联广告</title>

5</script>

22</style>

38</head>

39

40<body>

41<div id="mm">

42</div>

43</body>

44</html>

这个是可以正常运行的,只要你不使用文档类型声明。

但是,标准设计的网页需要进行文档类型声明以告知浏览器按照怎样的规则去解析网页。当我们使用过渡型标准声明的时候,我们发现这个原本工作正常的对联代码不再起作用。

这是符合标准的代码,和上面的区别是它加了文档类型声明:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

2<html xmlns="http://www.w3.org/1999/xhtml">

3<head>

4<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />

5<title>符合标准的不能正常工作的对联广告</title>

6</script>

23</style>

39</head>

40

41<body>

42<div id="mm">

43</div>

44</body>

45</html>

那么,为什么小小的标准声明让对联广告无法工作呢?

问题的根源:

google了几篇文章之后,找到了真凶!哦,抱歉,没有这么夸张了。

让我们回到第一段代码:

注意这一句:diffY=document.body.scrollTop;

当我们采用标准声明之后,你会发现无论你怎样拖动滚动条,diffY的值始终为零。见鬼了吗?不,事实上从html4/loose.dtd开始,只要你采用了相应的文档类型声明,diffY的值就会恒为零(有一种特殊情况,下面谈)。

为什么会这样?

因为采用标准的文档类型声明后,document.body.scrollTop已经无效,而应该用document.documentElement.scrollTop代替。

不仅仅是scrollTop有这种改变,更多请参加表(一)。

在表(一)中有这样一行:“Note: scrollLeft and scrollTop also work on DIV's with overflow: auto in Explorer 5+ on Windows and Mozilla 1.1”,这就是我所说的特殊情况。

怎么解决?

将第一段代码做一些修改:

var diffY;

if (document.documentElement && document.documentElement.scrollTop)

diffY = document.documentElement.scrollTop;

else if (document.body)

diffY = document.body.scrollTop

else

{/*Netscape stuff*/}

符合标准的正常工作的对联广告:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

2

3"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

4<html xmlns="http://www.w3.org/1999/xhtml">

5<head>

6<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />

7<title>符合标准的正常工作的对联广告</title>

8</script>

39

40</style>

56</head>

57

58<body>

59<div id="mm">

60</div>

61</body>

62</html>
以上代码在ie6.0,firefox1.5.0.3,opera7.23下测试通过。

ok,大功告成,干杯!

多说一句,DOCTYPE经常被人忽略,今天看来,实在不应该。

网站Flash对联广告源代码:

把下面这段代码保存为 adver-left.js

var imgheight

2var imgleft

3document.ns = navigator.appName == "Netscape"

4window.screen.width>800 ? imgheight=100:imgheight=100

5window.screen.width>800 ? imgleft=15:imgleft=122

6function myload()

7function leftmove()

21

36

37if (navigator.appName == "Netscape")

38else

42

把下面这段代码保存为 adver-right.js

var imgheight

2document.ns = navigator.appName == "Netscape"

3window.screen.width>800 ? imgheight=100:imgheight=100

4function myload()

5function mymove()

19

34MM_reloadPage(true)

40

41

42

43

44if (navigator.appName == "Netscape")

45else

48{

49document.write("<div id=dangdang style='position: absolute;width:80;top:0;left:578;visibility: visible;z-index: 1'><EMBED src='swf/00001.swf' quality=high WIDTH=80 HEIGHT=80 TYPE='application/x-shockwave-flash'></EMBED></div>");

50myload()

51}

网页中是这样调用,注意修改 js 文件的路径

<SCRIPT src="js/adver-left.js"></SCRIPT>

<SCRIPT src="js/adver-right.js"></SCRIPT>

上面两段代码中的 src='swf/00001.swf' ,改为自己广告的flash的路径
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: