[共同学习]贴段代码,大家来写执行结果,看谁理解更深刻,关于defer,attachEvent,window.onload....

Go_Rush 2006-04-19 01:01:43
<html>
<script>
window.attachEvent("onload",function(){alert('attach1')})
window.attachEvent("onload",function(){alert('attach2')})
</script>
<script>function window.onload(){alert('window.onload')}</script>
<script> alert('normal.1') </script>
<script defer> alert('defer1')</script>
<script defer> alert('defer2')</script>
<script> alert('normal.2') </script>

<SCRIPT event=onload for=window language=JavaScript>
alert('eventfor...')
</SCRIPT>

<body onload="javascript:alert('body onload=...')">
 
</body>
</html>

<script>alert('the last one')</script>


代码已经写得很清晰明了。
一个要求,先自己不要执行,看完代码立刻回贴写下上面代码的执行结果。
回贴后再检验自己的结果是否正确。 如果你写的结果不对,我们再聊聊。

上面代码最多最多可能弹出10个alert对话框。请把他们的弹出顺序依次写出来。

比如: 一定要在你调试之前写出来哦
attach1
attach2
window.onload
normal1
defer1
defer2
normal2
eventfor...
body onload=...
the last one...

如果你觉得某个语句不会弹出来,你就注明:(比如这样.)
defer2,normal2 不会出来


200分大贴,人人有分拿


上面的几乎是大部分常用的在页面加载完毕之后再执行script的代码
如果你能一次无调试的情况下写对的话,说明你很不错了
...全文
944 36 打赏 收藏 转发到动态 举报
写回复
用AI写文章
36 条回复
切换为时间正序
请发表友善的回复…
发表回复
kx_o1986 2010-06-10
  • 打赏
  • 举报
回复
好贴,得仔细研究一下。
zzblue 2006-06-09
  • 打赏
  • 举报
回复
好贴
  • 打赏
  • 举报
回复
调用meizz的代码

<script>
if(!window.attachEvent && window.addEventListener)
{
Window.prototype.attachEvent = HTMLDocument.prototype.attachEvent=
HTMLElement.prototype.attachEvent=function(en, func, cancelBubble)
{
var cb = cancelBubble ? true : false;
this.addEventListener(en.toLowerCase().substr(2), func, cb);
};
}

window.attachEvent("onunload", function(){alert(88)});
</script>
这样就不用再写针对其它的浏览器兼容代码了
大猫钓鱼 2006-04-21
  • 打赏
  • 举报
回复
好帖!下班了,回去再细看。^_^
ttyp 2006-04-21
  • 打赏
  • 举报
回复
有兴趣的可以再加几个试试,果然似乎是没有什么规律
ttyp 2006-04-21
  • 打赏
  • 举报
回复
Binds the specified function to an event, so that the function gets called whenever the event fires on the object.

Syntax

bSuccess = object.attachEvent(sEvent, fpNotify)
Parameters

sEvent Required. String that specifies any of the standard DHTML Events.
fpNotify Required. Pointer that specifies the function to call when sEvent fires.

Return Value

Boolean. Returns one of the following possible values:

true The function was bound successfully to the event.
false The function was not bound to the event.

Remarks

When sEvent fires on the object, the object's sEvent handler is called before fpNotify , the specified function. If you attach multiple functions to the same event on the same object, the functions are called in random order, immediately after the object's event handler is called.


注意:这里说attachEvent调用函数时是随机顺序的,于是再加几个测试看看

<html>
<script>
window.attachEvent("onload",function(){alert('attach1')})
window.attachEvent("onload",function(){alert('attach2')})
window.attachEvent("onload",function(){alert('attach3')})
window.attachEvent("onload",function(){alert('attach4')})
</script>
<script>function window.onload(){alert('window.onload')}</script>
<script> alert('normal.1') </script>
<script defer> alert('defer1')</script>
<script defer> alert('defer2')</script>
<script defer> alert('defer3')</script>
<script> alert('normal.2') </script>

<SCRIPT event=onload for=window language=JavaScript>
alert('eventfor...')
</SCRIPT>

<body onload="javascript:alert('body onload=...')">
 
</body>
</html>

<script>alert('the last one')</script>

结果很奇怪,defer也是类似的,另外对于event的调用会覆盖onload的调用也感觉比较奇怪,总之这个帖子不错
Scarroot 2006-04-21
  • 打赏
  • 举报
回复
mark
ilvs 2006-04-20
  • 打赏
  • 举报
回复
晕,,我错的够多的...

<SCRIPT event=onload for=window language=JavaScript>
alert('eventfor...')
</SCRIPT>

这么多种方法都没见过.........我觉得还是用常用的方法比较好.像DW, script默认的标签参数里不会有这样的代码提示的.
iuhxq 2006-04-20
  • 打赏
  • 举报
回复
不过我确实不明白javascript加载和执行顺序
iuhxq 2006-04-20
  • 打赏
  • 举报
回复
学习研究还行,实际根本没人用这种代码来开发
nakuspenda 2006-04-20
  • 打赏
  • 举报
回复
只有'window.onload' 不会弹出...
ilvs 2006-04-20
  • 打赏
  • 举报
回复
出现,attach1,attach2,normal.1,normai.2,eventfor...,body onload=...,the last one



'window.onload' ,defer1,defer2,这三个不会出来.
xuzuning 2006-04-20
  • 打赏
  • 举报
回复
normal.1
normal.2
defer2
defer1
the last.one
evntfor
attach2
attach1
和我预想的基本一致

不过不太明白 eventfor...和body onload=...为什么不出现
虽说
<SCRIPT event=onload for=window language=JavaScript>
alert('eventfor...')
</SCRIPT>

<body onload="javascript:alert('body onload=...')">
应该有一个被覆盖了,但另一个也还是应该有的

  • 打赏
  • 举报
回复
to meizz(梅花雪)

<script>
if(!window.attachEvent && window.addEventListener)
{
Window.prototype.attachEvent = HTMLDocument.prototype.attachEvent=
HTMLElement.prototype.attachEvent=function(en, func, cancelBubble)
{
var cb = cancelBubble ? true : false;
this.addEventListener(en.toLowerCase().substr(2), func, cb);
};
}
</script>

这个代码该怎么用?比如要触发onunload事件。
  • 打赏
  • 举报
回复
to meizz(梅花雪)

为何下面的程序,没有任何反应呢?哪里错了?
<script>
if(!window.attachEvent && window.addEventListener)
{
Window.prototype.attachEvent = HTMLDocument.prototype.attachEvent=
HTMLElement.prototype.attachEvent=function(en, func, cancelBubble)
{
var cb = cancelBubble ? true : false;
this.addEventListener(en.toLowerCase().substr(2), func, cb);
};
}
function func(){alert('the last one');}
</script>
xxuu503 2006-04-20
  • 打赏
  • 举报
回复
俺错了
  • 打赏
  • 举报
回复
支持一下
xxuu503 2006-04-20
  • 打赏
  • 举报
回复
1 normal.1
2 normal.2
3 body onload=...
4 eventfor...
5 defer1
6 defer2
7 the last one
cvpc 2006-04-20
  • 打赏
  • 举报
回复
好帖子!
Go_Rush 2006-04-20
  • 打赏
  • 举报
回复
再顶一下,午饭后结贴
加载更多回复(16)
内容概要:本文系统研究了构网型变流器的正负序阻抗解耦特性及其在弱电网环境下的稳定性表现,重点依托Matlab/Simulink仿真平台,构建了详细的阻抗数学模型,设计了解耦控制策略,并采用小信号扫频法进行频域辨识与稳定性验证。研究深入探讨了构网型变流器与传统跟网型逆变器在正负序阻抗特性上的本质差异,结合虚拟同步发电机(VSG)等先进控制技术,分析其在抑制宽频带振荡、削弱锁相环动态耦合等方面的优越性。文中不仅提供了完整的仿真模型与MATLAB代码实现,还整合了光伏、风电、储能、微电网等多类新能源系统的阻抗建模与稳定性分析资源,形成了一套面向新型电力系统稳定性的综合性技术资料体系,具有较强的科研复现与工程参考价值。; 适合人群:面向具备电力电子、电力系统自动化、新能源并网等专业背景的研究生、高校教师及工程技术人员,特别适用于从事阻抗建模、小干扰稳定性分析、宽频振荡机理研究以及撰高水平学术论文的科研工作者。; 使用场景及目标:①掌握构网型变流器正负序阻抗建模与扫频辨识的仿真方法;②深入理解VSG等构网型控制在弱电网中提升稳定性的内在机理;③复现顶刊论文中的阻抗分析流程与稳定性判据应用;④利用提供的成熟模型与代码加速科研进程,支撑课题研究与学术成果产出。; 阅读建议:建议结合文中提供的Simulink模型与MATLAB代码,按照“理论建模—仿真搭建—扫频激励—频响提取—Nyquist判据分析”的完整流程进行实践操作,重点关注扫频信号的注入方式、频率范围设置及阻抗曲线的物理意义解读,并参考博士论文复现案例深化对复杂动态耦合问题的理解

87,987

社区成员

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

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