8 个解决方案
#1
var x;
如果其他事件不用,完全可以销毁的。其他事件的第一行就进行销毁
#2
up
#3
这样不行啊,我有两个span,当鼠标移动span1上时调用变量x存了个信息,当鼠标移开时,调用了settimeout等待一秒钟读取变量x执行一个函数就over,问题是,在移开一秒前我又移到span2上,它会重新给x斌值,这样一来原先的settimeout执行函数时读取的x变量值就不对了啊?
#4
setTimeout的时候用个闭包把这个变量弄进去,外面的全局变量无论是改变了还是销毁了,都对这次操作无影响。。。
#5
可以定义一个全局变量,每次用到的时候就赋值,不用销毁也可以
#6
var x;
function mouseover{x=1;}
function mouseout{y=x;
setTimeout("f(y)",1000);
}
#7
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>
<BODY>
<span id="s1" style="background:#3fdddd;height:30px">222222222222222222</span>
<span id="s2" style="background:#654322;height:30px">333333333333333333</span>
<SCRIPT LANGUAGE="JavaScript">
<!--
function createEventFunc(type) {
var x;
if (type === "over") {
return function(event){
x = 1;
};
}
if (type === "out") {
return function(event){
x = null;
};
}
}
document.getElementById("s1").onmouseover = createEventFunc("over");
document.getElementById("s2").onmouseover = createEventFunc("over");
document.getElementById("s1").onmouseout = createEventFunc("out");
document.getElementById("s2").onmouseout = createEventFunc("out");
//-->
</SCRIPT>
</BODY>
</HTML>
#8
上面的不行,这个试下
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
</HEAD>
<BODY>
<span id="s1" style="background:#3fdddd;height:30px">222222222222222222</span>
<span id="s2" style="background:#654322;height:30px">333333333333333333</span>
<SCRIPT LANGUAGE="JavaScript">
<!--
function Func() {
var obj = {
over:function(event){
alert(this.x);
this.x = 1;
},
out:function(event){
alert(this.x);
this.x = undefined;
}
};
this.createEventFunc = function(type) {
if (type === "over") {
return obj.over;
}
if (type === "out") {
return obj.out;
}
}
}
var func = new Func();
document.getElementById("s1").onmouseover = func.createEventFunc("over");
document.getElementById("s2").onmouseover = func.createEventFunc("over");
document.getElementById("s1").onmouseout = func.createEventFunc("out");
document.getElementById("s2").onmouseout = func.createEventFunc("out");
//-->
</SCRIPT>
</BODY>
</HTML>
#1
var x;
如果其他事件不用,完全可以销毁的。其他事件的第一行就进行销毁
#2
up
#3
这样不行啊,我有两个span,当鼠标移动span1上时调用变量x存了个信息,当鼠标移开时,调用了settimeout等待一秒钟读取变量x执行一个函数就over,问题是,在移开一秒前我又移到span2上,它会重新给x斌值,这样一来原先的settimeout执行函数时读取的x变量值就不对了啊?
#4
setTimeout的时候用个闭包把这个变量弄进去,外面的全局变量无论是改变了还是销毁了,都对这次操作无影响。。。
#5
可以定义一个全局变量,每次用到的时候就赋值,不用销毁也可以
#6
var x;
function mouseover{x=1;}
function mouseout{y=x;
setTimeout("f(y)",1000);
}
#7
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>
<BODY>
<span id="s1" style="background:#3fdddd;height:30px">222222222222222222</span>
<span id="s2" style="background:#654322;height:30px">333333333333333333</span>
<SCRIPT LANGUAGE="JavaScript">
<!--
function createEventFunc(type) {
var x;
if (type === "over") {
return function(event){
x = 1;
};
}
if (type === "out") {
return function(event){
x = null;
};
}
}
document.getElementById("s1").onmouseover = createEventFunc("over");
document.getElementById("s2").onmouseover = createEventFunc("over");
document.getElementById("s1").onmouseout = createEventFunc("out");
document.getElementById("s2").onmouseout = createEventFunc("out");
//-->
</SCRIPT>
</BODY>
</HTML>
#8
上面的不行,这个试下
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
</HEAD>
<BODY>
<span id="s1" style="background:#3fdddd;height:30px">222222222222222222</span>
<span id="s2" style="background:#654322;height:30px">333333333333333333</span>
<SCRIPT LANGUAGE="JavaScript">
<!--
function Func() {
var obj = {
over:function(event){
alert(this.x);
this.x = 1;
},
out:function(event){
alert(this.x);
this.x = undefined;
}
};
this.createEventFunc = function(type) {
if (type === "over") {
return obj.over;
}
if (type === "out") {
return obj.out;
}
}
}
var func = new Func();
document.getElementById("s1").onmouseover = func.createEventFunc("over");
document.getElementById("s2").onmouseover = func.createEventFunc("over");
document.getElementById("s1").onmouseout = func.createEventFunc("out");
document.getElementById("s2").onmouseout = func.createEventFunc("out");
//-->
</SCRIPT>
</BODY>
</HTML>