为什么attachevent没有生效?

woodzong 2008-02-03 11:46:40
html页面代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<script language="javascript">var G_iStartTime = (new Date()).getTime();</script>
<title>test</title>
</head>
<body>
<script type="text/javascript" src="http://www.paipai.com/js/elf_comm.js?t=20071211"></script>
<script>
function testfun(){
alert(1);
}
document.body.attachEvent("onload",testfun);
</script>
测试文本。
</body>
</html>

这样以后testfun是不会执行的。
但我修改成
document.body.onload = testfun;
就可以执行testfun, 请问是什么原因呢?
...全文
320 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
he_8134 2008-02-05
  • 打赏
  • 举报
回复
噢 反正ff中不是bool就行了....
he_8134 2008-02-05
  • 打赏
  • 举报
回复
火狐貌似也有document.all吧...
Phenlit 2008-02-05
  • 打赏
  • 举报
回复
2楼的,在FF下绑定事件调用函数漏了括号,必须加上才能用的,如testfun()

<script>
function testfun(){
alert(1);
}
if(document.all){
window.attachEvent("onload",testfun);
}else{
window.addEventListener("load",testfun(), false);
}
yixianggao 2008-02-04
  • 打赏
  • 举报
回复
要兼容FF2,需要修改language属性值为JavaScript

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> new document </title>
<meta name="generator" content="editplus" />
<meta name="author" content="Gao YiXiang" />
<meta name="email" content="yixianggao@126.com" />
<meta name="keywords" content="javascript dhtml dom" />
<meta name="description" content="I love web development." />
</head>
<body>
测试文本。
<SCRIPT FOR="window" EVENT="onload" LANGUAGE="JavaScript">
alert(1);
</SCRIPT>
</body>
</html>
yixianggao 2008-02-04
  • 打赏
  • 举报
回复
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> new document </title>
<meta name="generator" content="editplus" />
<meta name="author" content="Gao YiXiang" />
<meta name="email" content="yixianggao@126.com" />
<meta name="keywords" content="javascript dhtml dom" />
<meta name="description" content="I love web development." />
</head>
<body>
测试文本。
<SCRIPT FOR="window" EVENT="onload" LANGUAGE="JScript">
alert(1);
</SCRIPT>
</body>
</html>


DHTML手册里详细讲解了原因:
请仔细阅读第一段后半段和第二段全部!
L@_@K
onload Event

The browser loads applications, embedded objects, and images as soon as it encounters the applet, embed, and img objects during parsing. Consequently, the onload event for these objects occurs before the browser parses any subsequent objects. To ensure that an event handler receives the onload event for these objects, place the script object that defines the event handler before the object and use the onload attribute in the object to set the handler.

The onload attribute of the body object sets an onload event handler for the window. This technique of calling the window onload event through the body object is overridden by any other means of invoking the window onload event, provided the handlers are in the same script language.


送你三本手册!

DHTML参考手册
http://download.csdn.net/source/308913

样式表中文手册
http://download.csdn.net/source/304124

JScript语言参考
http://download.csdn.net/source/308916
hsx85315 2008-02-04
  • 打赏
  • 举报
回复

......
function testfun()
{
alert(1);
}
document.body.attachEvent("onload",testfun);
//此时document.body.onload 仍为 null

document.body["onload"] = testfun;
//代码运行到此 document.body.onload 才有真正的内容

</script>
测试文本。
</body>
</html>
xiaolei1982 2008-02-03
  • 打赏
  • 举报
回复
window.attachEvent("onload",testfun);
nicholsky 2008-02-03
  • 打赏
  • 举报
回复
document.body.attachEvent还没见识过这样的用法。一般的事件绑定都是document和windows的方法。前者包括所有的onmousexxxx、onkeyxxx等,后者一般是onload等属于window的方法的绑定。
slimboy123 2008-02-03
  • 打赏
  • 举报
回复
楼上正解


<script>
function testfun(e){
alert(1);
}
if (document.body.addEventListener) {
window.addEventListener( "load", testfun, false );
}
else if (document.body.attachEvent) {
window.attachEvent( "onload", testfun );
}
</script>
cloudgamer 2008-02-03
  • 打赏
  • 举报
回复
function testfun(){ 
alert(1);
}

if(document.all){
window.attachEvent("onload",testfun);
}else{
window.addEventListener("load", testfun, false);
}


支持ff
目录 IBM WEBSPHERE PORTAL 1 目录 2 参数资料 10 一、搜索功能 11 1、搜索表单 11 2、搜索组件 11 3、搜索集合 12 4、站点设置 13 5、模板 13 6、站区创建及内容 13 7、搜索跳转页面 14 8、搜索表单的部署 14 9、详细内容页面 14 10、其他组件 14 11、搜索功能会遇到的问题及其解决方案 14 12、搜索过滤 16 13、注意 16 14、WCM高级搜索参数 16 ADVANCED WCM/WEBSPHERE PORTAL SEARCH INTEGRATION 17 Introduction 17 How to search WCM content using this metadata 18 Specific example 21 15、高级参数二 32 16、搜索网页资料 33 二、页面导航 41 1、页面导航组件创建 41 2、使用场景及其环境设置 41 三、授权与用户 41 1、一般用户 41 2、匿名用户 42 3、授权地点及其方式 42 4、传播与继承 42 5、用户与组管理 43 四、域名 44 1、portal服务器域名 44 2、本机域名映射 44 五、WCM几个页面路径及其名称 44 1、登录页面 44 2、主题及主页页面 45 3、皮肤外观页面路径 45 4、session超时页面 45 5、登录portal时,绕过登录界面,直接进入到主页(或再登录) 45 6、自定义登录跳转页面 45 7、判断用户是否登录标签 46 8、portal登录注销再登录只出现一张图片 46 9、登录失败及修改提示信息 46 10、设置超时跳转页面 47 11、 输出登录用户名称 47 12、输出注销 48 13 、动态链接 48 六、使用WCM时,会遇到的问题 48 1、不能向富文本编辑器中粘贴内容 48 2、设置WCM timeout超时值 48 3、一般用户不能访问内容 49 4、页面导航不能分页 49 5、一些portal常见问题 49 6、Portal中调用PUMA SPI管理用户和组信息 51 7、WCM内存溢出的解决方法 53 七、组件及其标签 53 (一)、标签 53 1、jsp组件 53 2、组件关联格式 54 3、IDCmpnt标签 55 4、workflowcmpnt标签 55 5、securitycmpnt标签 58 6、profilecmpnt 标签 63 7、pathcmpnt 标签 65 8、historycmpnt标签 67 9、idcmpnt标签 70 10、数字显示格式 73 11、URLCmpnt标签 74 12、AptrixLibCmpnt 标签 76 13、placeholder标签与用户名显示 77 14、JSP中初始化workspace的标签 78 15、JSP中用标签输出一条内容 78 (二)、标签及其功能的使用 78 1、Writing links to Web Content Management content 78 (三)拖放 JSP标记 80 1) 80 2) 83 3) 84 (四)拖放区域 84 1)在定制主题和外表中使用拖放 85 2)拖放标记摘要 85 3)拖放 API 86 八、功能应用或问题 87 1、WCM多子站区指向指定子站区配置 87 2、更改PORTAL默认的登录选项 89 3、如何在主题与皮肤中获取PORTAL登录用户信息 90 4、PORTAL6.1.0.3无法查找任何的用户或用户组 92 5、如何个性化PORTLET添加控制板 93 6、IBM WCM中匿名搜索 94 7、开发一个系统登录的PORTLET 95 8、WEBSPHERE PORTAL调整PORTLET之间的间隙 102 9、PORTAL6.1.0.1配置扩展属性 105 10、内容链接(同服务器或同站区) 120 11、内容链接(不同服务器或不同站区) 120 12、草稿内容阶段的判断、操作和获取(API) 121 13、WCM 中附件下载 121 14、WCM流程带审核组成 122 15、关于使用WCM API获取内容的小经验 123 16、PORTAL WAR包部署方式 124 17、设置PAGE-AREA区域背景为透明 127 18、设置PAGE-AREA区域的宽度 127 19、设置左侧导航页面相对标签的缩进 128 20、PORTLET之间间隙的调整 129 21、WCM 主题中获取登录用户的UID信息 129 22、配置是否自动加载主题JSP文件 131 23、菜单中标题链接迭代方式(

87,907

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧