function a() {
this.name = "a";
var me = this;
setInterval(function(){time1(me)}, 1000 );
}

解决方案 »

  1.   

    setInterval这种是系统调用的方法
    不能直接用this
      

  2.   

    好像是不能用 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>
      

  3.   

    var at = a();
    var bt = b();
    ==》
    var at = new a();
    var bt = new b();既然你已经使用了 this 这样的类写法,那你调用类的时候就应该用 new 来新开实例!
      

  4.   

    <!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>
      

  5.   

    是啊,这样做已经背离了this的初衷
      

  6.   

    再说你也用不着那样写类吧,象这样写,多方便哪!!<!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>
      

  7.   

    呵呵,原来是酱紫,受教
    javascript的函数,类都刚开始学习
    有什么好的介绍这些方面的书么?
    在csdn找了个中文版javascript权威指南
    结果下载了解压缩错误:(
    不知道各位谁有中文版的