<!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" />
<title></title>
</head><body>
<div id="tip" style="display:none">提示的内容!</div>
<script language="javascript">
//
// 2010-1-4 14:48:21var tip={
obj:0,//DIV 或者SPAN 对象
start:2,//开始的秒数
len:5,//显示的秒数
tip:function(_obj,_start,_len){
this.obj = _obj;
_start && this.start = _start;
_len && this.len = _len;
setTimeout('display(this.obj)',this.start*1000);
setTimeout('display(this.obj)',(this.start+this.len)*1000);
}
};function display(obj){
var s = obj.style;
s.display = s.display ? '':'none';
} //使用
tip.tip(document.getElementById('tip'),2,5);</script>
</body>
</html>总是有错误,因为时间紧迫,所以换了另外一种方法了。
大家帮我看下为什么会错误,这个函数的功能就是当页面载入多少秒后这个div显示,多少秒后div自动隐藏。
顺便评论我的代码是不是规范的JavaScript。
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title></title>
</head><body>
<div id="tip" style="display:none">提示的内容!</div>
<script language="javascript">
//
// 2010-1-4 14:48:21var tip={
obj:0,//DIV 或者SPAN 对象
start:2,//开始的秒数
len:5,//显示的秒数
tip:function(_obj,_start,_len){
this.obj = _obj;
_start && this.start = _start;
_len && this.len = _len;
setTimeout('display(this.obj)',this.start*1000);
setTimeout('display(this.obj)',(this.start+this.len)*1000);
}
};function display(obj){
var s = obj.style;
s.display = s.display ? '':'none';
} //使用
tip.tip(document.getElementById('tip'),2,5);</script>
</body>
</html>总是有错误,因为时间紧迫,所以换了另外一种方法了。
大家帮我看下为什么会错误,这个函数的功能就是当页面载入多少秒后这个div显示,多少秒后div自动隐藏。
顺便评论我的代码是不是规范的JavaScript。
setTimeout(function() {display(me.obj)},this.start*1000);
setTimeout(function() {display(me.obj)},(this.start+this.len)*1000);
_len && this.len = _len;--》 _start && (this.start = _start);
_len && (this.len = _len);
<!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" />
<title></title>
</head><body>
<div id="tip" style="display:none">提示的内容!</div>
<script language="javascript">
//
// 2010-1-4 14:48:21var tip={
obj:0,//DIV 或者SPAN 对象
start:2,//开始的秒数
len:5,//显示的秒数
tip:function(_obj,_start,_len){
this.obj = _obj;
this.start = _start;
this.len = _len;
setTimeout("tip.display()",this.start*1000);
setTimeout("tip.display()",(this.start+this.len)*1000);
},
display: function(){
this.obj.style.display = this.obj.style.display ? '':'none';
}
};//使用
tip.tip(document.getElementById('tip'),2,5);</script>
</body>
</html>
我就是想,有可能在使用tip.tip()的时候,可能是这样的
tip.tip(document.getElementById('tip'));
,那么就默认用原来的那个时间。
你的这个代码不能这样。
另外一个函数假如他有三个参数,但是我使用的时候只写了两个,而JavaScript不能像PHP那样直接在函数名称的参数那里写初始值,即function PHPfunc($a='b',$c='d'){}
setTimeout(function(){tip.display()},this.start*1000);上下2个都是
明明是对的。。不要误导别人。tip是全局变量,有什么错?不信你可以运行一下。当然你的写法也正确。
JS里面定义参数默认值一般是这样的
function test(a,b,c) {
a = a||'defaultValue Of a';
b = b||'defaultValue Of b';
c = c||'defaultValue Of c'; document.write('a:'+a+'<br/>b:'+b+'<br/>c:'+c+'<br/><br/>');
}test();
test('new Value Of a');
test('new Value Of a','new Value Of b');再扯代码规范性:你面向对象的思想应该再提高提高。一个对象的方法和属性应该集中起来。你把display方法拆了出来。这个是不对的。
function fun1(arg1,arg2){
return function(){
//这里可以用参数了
}
}
我的说法是针对他当前的用法——display方法没有参数,tip是全局变量。
你说的也对。这里使用匿名函数更多是因为传入的变量是函数的局部变量的情况
不过建议尽量读 E 文版,csdn 下载里有免费 pdf 的!程序员需要 Code Sence,lz 的代码中根本看不到,需要慢慢培养!
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title></title>
</head><body>
<div id="tip" style="display:none">提示的内容!</div>
<script language="javascript">
//
// 2010-1-4 14:48:21var tip={
obj:0,//DIV 或者SPAN 对象
start:2,//开始的秒数
len:5,//显示的秒数
tip:function(_obj,_start,_len){
that = this;
this.obj = _obj;
this.start = _start || this.start;
this.len = _len || this.len;
setTimeout('display(that.obj)',this.start*1000);
setTimeout('display(that.obj)',(this.start+this.len)*1000);
}
};function display(obj){ var s = obj.style;
s.display = s.display ? '':'none';
} //使用
tip.tip(document.getElementById('tip'),2,5);</script>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title></title>
</head><body>
<div id="tip" style="display:none">提示的内容!</div>
<script language="javascript">
//
// 2010-1-4 14:48:21var tip={
obj:0, //DIV 或者SPAN 对象
start:2, //开始的秒数
len:5, //显示的秒数
tip:function(_obj,_start,_len){
this.obj = _obj;
//这里的两个&&不知道是做什么的
// _start && this.start = _start;
// _len && this.len = _len;
this.start = _start;
this.len = _len;
//这里的参数以前没有传进去要改了
setTimeout(display(this.obj),this.start*1000);
setTimeout(display(this.obj),(this.start+this.len)*1000);
}
};
//这个函数改了一下
function display(obj) {
return function() {
var s = obj.style;
s.display = s.display ? '' : 'none';
}
} //使用
tip.tip(document.getElementById('tip'),2,5);</script>
</body>
</html>