渐变的:
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>New Page 1</title><script language=javascript>function high(w)
{
obj=w; //******************************
highlighting=setInterval("t(obj);",50);
}function low(w)
{
clearInterval(highlighting);
w.filters.alpha.opacity=20;
}function mylow(w)
{
obj=w; //*******************
if(w.filters.alpha.opacity>20)
{
clearInterval(highlighting);
myint=setInterval("u(obj);",200);
}
}function t(c)
{
if(c.filters.alpha.opacity<100) c.filters.alpha.opacity+=5;
else if(window.highlighting) clearInterval(highlighting);
}function u(c)
{
if(c.filters.alpha.opacity>20) c.filters.alpha.opacity-=5;
else clearInterval(myint);
}
</script></head><body>
<img src=1.jpg width=400 height=300 style="filter:alpha(opacity=40)" OnMouseOver="high(this);" OnMouseOut="mylow(this);">
</body></html>
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>New Page 1</title><script language=javascript>function high(w)
{
obj=w; //******************************
highlighting=setInterval("t(obj);",50);
}function low(w)
{
clearInterval(highlighting);
w.filters.alpha.opacity=20;
}function mylow(w)
{
obj=w; //*******************
if(w.filters.alpha.opacity>20)
{
clearInterval(highlighting);
myint=setInterval("u(obj);",200);
}
}function t(c)
{
if(c.filters.alpha.opacity<100) c.filters.alpha.opacity+=5;
else if(window.highlighting) clearInterval(highlighting);
}function u(c)
{
if(c.filters.alpha.opacity>20) c.filters.alpha.opacity-=5;
else clearInterval(myint);
}
</script></head><body>
<img src=1.jpg width=400 height=300 style="filter:alpha(opacity=40)" OnMouseOver="high(this);" OnMouseOut="mylow(this);">
</body></html>
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>New Page 1</title>
<script language=javascript>
var h; //*************************
function guanbi()
{
h.close(); //*******************
}
function gogo()
{
h=window.open('http://www.sohu.com','',''); //******************
setTimeout("guanbi();",2000);
}
</script>
</head><body onload="gogo();"></body></html>
gogo()函数是这样:
function gogo(arg)
{
setInterval("fun(arg);",50); //想把这个arg,既那张图片传到fun()函数中;
}......................
A:你这样参数arg并没有传到fun()里。setInterval()中第一个参数是字符串,在运行时才解析,
比如你下成setInterval("fun(aaa",1000)在没运行时也不会报错的。你这种情况,应该写成,
setInterval("fun("+arg+")",50).在你的例子里,你先写了个obj=arg,然后才可以执行实际上是定义了个全局变量。
再说,加句obj=arg,就算是设了个全局变量,但它也在字符串中啊,为什么就能正确传递呢?已经赋过值的内部变量和全局变量做为函数的值传递参数向函数内传值,效果应该是一样的,只有不通过参数,直接在函数内调用全局变量才能体现出全局变量的作用。
还有,如果我换成setTimeout("fun(arg)",1000),即使用obj=arg;setTimeout("fun(obj)");来替换,照样提示obj未定义,难道这里的obj就不是全局变量了吗继续郁闷!
一个解决办法就是上面说过的,通过全局变量传值。
<script language=javascript>
var obj;
function gogo(arg1)
{
obj=arg1;
setInterval("fun(obj)",1000);//这里用setTimeout一样的,你怎么说不行?
}
function fun(arg2){
statements; //这里是一些用arg2的语句
}
</script>
当然,不传值直接对全局变量操作也是一样,如
function gogo(arg1)
{
obj=arg1;
setInterval("fun()",1000);//这里用setTimeout一样的,你怎么说不行?
}
function fun(){
statements; //这里是一些直接用全局变量obj的语句
}另外,要说明的是,在function里,未显式用var定义,而直接使用的变量,作为全局变量对待,用var显式声明的则是内部变量。
<script language=javascript>
var obj; //****************************************************
function gogo(arg1)
{
obj=arg1;
setInterval("fun(obj)",1000);//这里用setTimeout一样的,你怎么说不行?
}
function fun(arg2){
statements; //这里是一些用arg2的语句
}
</script>我的贴可没有加上一串星的那句啊,要是那样的话早就没有问题了.你把那句删除了再测,有意思的很啊!