/*
在ie中,事件对象是作为一个全局变量来保存和维护的。 所有的浏览器事件,不管是用户触发
的,还是其他事件, 都会更新window.event 对象。 所以在代码中,只要轻松调用 window.event
就可以轻松获取 事件对象, 再 event.srcElement 就可以取得触发事件的元素进行进一步处理
在ff中, 事件对象却不是全局对象,一般情况下,是现场发生,现场使用,ff把事件对象自动传
递给对应的事件处理函数。 在代码中,函数的第一个参数就是ff下的事件对象了。
*/
<input type="button" onclick="test(arguments[0])" value="run test" />
<button id="btn4" onclick="foo4()">按钮4</button>
<button id="btn" onclick="foo()">按钮0</button>
<button id="btn1">按钮1</button>
<button id="btn2">按钮2</button>
<button id="btn3">按钮3</button>
function Person(){}
Person.prototype = {
initialize:function(){this.name="BOY";},
GetName:function(){alert(this.name);},
AjaxQuestion:function()
{
var option={
method:"get",
parameters:"",
onSuccess:function(transport){
alert("hello");
var self = this;
self.GetName();
alert("hello2");
},
onFailure:function(transport){
alert("failure");
}
}
// var request = new Ajax.Request("Test.aspx",option);
}
}
var ps =new Person();
ps.initialize();
ps.GetName();
function test(evt)
{
evt = event || evt;
alert(evt);
}
function foo4(){
// var evt=getEvent()
var evt = SearchEvent()
var element=evt.srcElement || evt.target
alert(element.id)
}
function getEvent(){ //同时兼容ie和ff的写法
if(document.all) return window.event;
func=getEvent.caller;
while(func!=null){
var arg0=func.arguments[0];
if(arg0){
if((arg0.constructor==Event || arg0.constructor ==MouseEvent)//arg0.constructor 是等于 MouseEvent 的
//用 arg0.constructor==Event 当然取不到
|| (typeof(arg0)=="object" && arg0.preventDefault && arg0.stopPropagation)){
return arg0;//
}
}
func=func.caller;
}
return null;
}
function SearchEvent()
{
//IE
if(document.all)
return window.event;
func=SearchEvent.caller;
while(func!=null)
{
var arg0=func.arguments[0];
if(arg0)
{
if(arg0.constructor==Event)
return arg0;
}
func=func.caller;
}
return null;//not found
}
//Professional Javascript for Web Developers
function foo(){
alert(arguments[0] || window.event)
}
/*
原因在于 事件绑定的方式
onclick="foo()" 就是直接执行了, foo() 函数,没有任何参数的,
这种情况下 firefox没有机会传递任何参数给foo
而 btn.onclick=foo 这种情况, 因为不是直接执行函数,firefox才有机会传参数给foo
*/
//--------------------------------------------------------------
window.onload=function(){
document.getElementById("btn1").onclick=foo1
document.getElementById("btn2").onclick=foo2
document.getElementById("btn3").onclick=foo3
}
function foo1(){
//ie中, window.event使全局对象
alert(window.event) // ie下,显示 "[object]" , ff下显示 "undefined"
//ff中, 第一个参数自动从为 事件对象
alert(arguments[0]) // ie下,显示 "undefined", ff下显示 "[object]"
}
function foo2(e){
alert(window.event) // ie下,显示 "[object]" , ff下显示 "undefined"
//注意,我从来没有给 foo2传过参数哦。 现在 ff自动传参数给 foo2, 传的参数e 就是事件对象了
alert(e) // ie下,显示 "undefined", ff下显示 "[object]"
}
function foo3(){ //同时兼容ie和ff的写法,取事件对象
alert(arguments[0] || window.event) // ie 和 ff下,都显示 "[object]"
var evt=arguments[0] || window.event
var element=evt.srcElement || evt.target //在 ie和ff下 取得 btn3对象
alert(element.id) // btn3
}

浙公网安备 33010602011771号