作用域的问题 function a() { this.name = "a"; var me = this; setInterval(function(){time1(me)}, 1000 );} 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 setInterval这种是系统调用的方法不能直接用this 好像是不能用 this加上一个局部变量还是遇到相同的问题有其他解决方法么?-----------------------------------------------------------<!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"><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>test</title></head><body><fieldset><legend>a</legend><p id="a1"></p></fieldset><fieldset><legend>b</legend><p id="b1"></p></fieldset><script language="javascript">function $(s){return document.getElementById(s);}function a() {this.name = "a";var me = this;setInterval( function(){time1(me)}, 1000 );}function time1(o){var d = new Date();$("a1").innerHTML = "(a) : " + o.name + " - " + d.getSeconds();}function b() {this.name = "b";var me = this;setInterval( function(){time2(me)}, 1000 );}function time2(o){var d = new Date();$("b1").innerHTML = "(b) : " + o.name + " - " + d.getSeconds();}var at = a();var bt = b();</script></body></html> var at = a();var bt = b();==》var at = new a();var bt = new b();既然你已经使用了 this 这样的类写法,那你调用类的时候就应该用 new 来新开实例! <!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"><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>test</title></head><body><fieldset><legend>a</legend><p id="a1"></p></fieldset><fieldset><legend>b</legend><p id="b1"></p></fieldset><script language="javascript">function $(s){return document.getElementById(s);}function a() {this.name = "a";var me = this;setInterval( function(){time1(me)}, 1000 );}function time1(o){var d = new Date();$("a1").innerHTML = "(a) : " + o.name + " - " + d.getSeconds();}function b() {this.name = "b";var me = this;setInterval( function(){time2(me)}, 1000 );}function time2(o){var d = new Date();$("b1").innerHTML = "(b) : " + o.name + " - " + d.getSeconds();}var at =new a();var bt =new b();</script></body></html> 是啊,这样做已经背离了this的初衷 再说你也用不着那样写类吧,象这样写,多方便哪!!<!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"><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>test</title></head><body><fieldset><legend>a</legend><p id="a1"></p></fieldset><fieldset><legend>b</legend><p id="b1"></p></fieldset><script language="javascript">function $(s){return document.getElementById(s);}function mm(name){ this.name = name; var me = this; setInterval( function(){me.timer()}, 1000 );}mm.prototype.timer=function(){ var d = new Date(); $(this.name +"1").innerHTML = "("+ this.name +") : " + this.name + " - " + d.getSeconds();}var at = new mm("a");var bt = new mm("b");</script></body></html> 呵呵,原来是酱紫,受教javascript的函数,类都刚开始学习有什么好的介绍这些方面的书么?在csdn找了个中文版javascript权威指南结果下载了解压缩错误:(不知道各位谁有中文版的 急求:Ext中src中文问题 无函数名的函数 关于导航栏 怎么做图片向右连续滚动的效果啊? 下载文件保存问题,在线等待,解决即给分 愁死我了 交换图片点击后不再置换的问题 请问用jquery如何在ajax post的返回处理函数中用submit提交表单呢? JS 替换指定字符串中遇到的问题 java web 添加数据问题 asp.net 中用Javascript的对联式广告怎么不能用呀 ShowmodalDialog中使用window.open,确不能弹出新窗口
不能直接用this
加上一个局部变量还是遇到相同的问题
有其他解决方法么?
-----------------------------------------------------------<!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">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
</head>
<body><fieldset>
<legend>a</legend>
<p id="a1"></p>
</fieldset>
<fieldset>
<legend>b</legend>
<p id="b1"></p>
</fieldset><script language="javascript">
function $(s){
return document.getElementById(s);
}function a() {
this.name = "a";
var me = this;
setInterval( function(){time1(me)}, 1000 );
}
function time1(o){
var d = new Date();
$("a1").innerHTML = "(a) : " + o.name + " - " + d.getSeconds();
}function b() {
this.name = "b";
var me = this;
setInterval( function(){time2(me)}, 1000 );
}
function time2(o){
var d = new Date();
$("b1").innerHTML = "(b) : " + o.name + " - " + d.getSeconds();
}var at = a();
var bt = b();
</script></body>
</html>
var bt = b();
==》
var at = new a();
var bt = new b();既然你已经使用了 this 这样的类写法,那你调用类的时候就应该用 new 来新开实例!
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
</head>
<body><fieldset>
<legend>a</legend>
<p id="a1"></p>
</fieldset>
<fieldset>
<legend>b</legend>
<p id="b1"></p>
</fieldset><script language="javascript">
function $(s){
return document.getElementById(s);
}function a() {
this.name = "a";
var me = this;
setInterval( function(){time1(me)}, 1000 );
}
function time1(o){
var d = new Date();
$("a1").innerHTML = "(a) : " + o.name + " - " + d.getSeconds();
}function b() {
this.name = "b";
var me = this;
setInterval( function(){time2(me)}, 1000 );
}
function time2(o){
var d = new Date();
$("b1").innerHTML = "(b) : " + o.name + " - " + d.getSeconds();
}var at =new a();
var bt =new b();
</script></body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
</head>
<body><fieldset>
<legend>a</legend>
<p id="a1"></p>
</fieldset>
<fieldset>
<legend>b</legend>
<p id="b1"></p>
</fieldset><script language="javascript">
function $(s){return document.getElementById(s);}function mm(name)
{
this.name = name;
var me = this;
setInterval( function(){me.timer()}, 1000 );
}
mm.prototype.timer=function()
{
var d = new Date();
$(this.name +"1").innerHTML = "("+ this.name +") : " + this.name + " - " + d.getSeconds();
}var at = new mm("a");
var bt = new mm("b");
</script></body>
</html>
javascript的函数,类都刚开始学习
有什么好的介绍这些方面的书么?
在csdn找了个中文版javascript权威指南
结果下载了解压缩错误:(
不知道各位谁有中文版的