insertAdjacentHTML()很好用,可以不用attachEvent阿

解决方案 »

  1.   

    我是用在 htc 中的,有的时候好像必须绑定函数句柄。
    其实我想传的主要就是this,有没有办法获取这个事件绑定的对象(不是srcElement)?
      

  2.   

    判断event.srcElement.name或Id即可,不要加this了
      

  3.   

    这样就没有通用性了,
    比如说
    <table>
      <tr>
        <td onclick="fn()">
          <table>....<input>...
          </table>
        </td>
        <td onclick="fn()"></td>
      </tr>
    </table>要是有人点了input,我在fn()中怎样处理?我的tr,td以及里面的html元素都是用程序生成的。
      

  4.   

    这是我写的东西:
    不过在低版本的JS会出现非法操作。例如想attach theobj.MyOnEvent("aaa")
    那么:
    element.attachEvent("onclick",Delegate(theobj,"MyOnEvent").PreInvoke("aaa"));
    ------------------------------------------------------------
    function FunctionSelf(){return FunctionSelf.caller;}
    function Delegate(obj,funcOrName)
    {
    var delegate=new Function("","return FunctionSelf().Invoke(FunctionSelf().arguments)"); delegate.Arguments=new Array();
    delegate.Object=obj;
    delegate.UniqueName="DelegateUniqueID"+Math.floor((new Date().getTime()+Math.random())*1000);
    if(typeof(funcOrName)=="string")
    {
    delegate.FuncName=funcOrName;
    delegate.Function=obj[delegate.FuncName];
    }
    else
    {
    delegate.FuncName=null;
    delegate.Function=funcOrName;
    } delegate.Invoke=Delegate.Invoke;
    delegate.Detach=Delegate.Detach;
    delegate.SetArguments=Delegate.SetArguments;
    delegate.PreInvoke=Delegate.PreInvoke;
    delegate.valueOf=new Function("","return \""+delegate.UniqueName+"\"");
    delegate.toString=new Function("","return \""+delegate.UniqueName+"\""); return delegate;
    }
    function Delegate.GetCaller()
    {
    var cid=FunctionSelf().caller.caller.CallID;
    return Delegate.Coll[cid];
    }
    function Delegate.GetDelegate()
    {
    var cid=FunctionSelf().caller.caller.CallID;
    return Delegate.Coll[cid].Delegate;
    }
    function Delegate.PreInvoke()
    {
    this.SetArguments(Delegate.PreInvoke.arguments);
    return this;
    }
    function Delegate.SetArguments(args)
    {
    if(args==null)args=new Array();
    this.Arguments=new Array();
    for(var i=0;i<args.length;i++)this.Arguments[i]=args[i];
    return this;
    }
    function Delegate.Invoke(args)
    {
    if(this.Object==null)return;
    var cid=Delegate.Coll.Insert(this,args);
    var strArguments="";
    var i=0;
    for(i=0;i<args.length;i++)
    {
    strArguments+="Delegate.Coll['"+cid+"'].Arguments["+i+"]";
    if(i<args.length-1)strArguments+=",";
    }
    if(i>0&&i<this.Arguments.length)strArguments+=",";
    for(;i<this.Arguments.length;i++)
    {
    strArguments+="Delegate.Coll['"+cid+"'].Delegate.Arguments["+i+"]";
    if(i<this.Arguments.length-1)strArguments+=",";
    } var funcName=this.FuncName||cid;
    if(this.FuncName==null)this.Object[funcName]=this.Function;
    var res;
    var exception;
    try
    {
    res=eval("new Function('',\"FunctionSelf().CallID='"+cid+"';return Delegate.Coll['"+cid+"'].Delegate.Object['"+funcName+"']("+strArguments+")\")()");
    }catch(x){exception=x}
    if(this.Object&&(this.FuncName==null))delete this.Object[funcName];
    Delegate.Coll.Remove(cid);
    if(exception)throw(exception);
    return res;
    }
    function Delegate.Detach()
    {
    this.Object=null;
    this.Function=null;
    this.FuncName=null;
    this.UniqueName=null;
    }
    function Delegate.EvalCaller(delegate,args,cid)
    {
    this.Delegate=delegate;
    this.Arguments=args;
    this.CallID=cid;
    }
    function Delegate.Coll(){}
    function Delegate.Coll.Insert(delegate,args)
    {
    if(typeof(Delegate.Coll.Length)=="undefined")Delegate.Coll.Length=0;
    Delegate.Coll.Length++;
    var cid=delegate.UniqueName+"call"+Math.floor((new Date().getTime()+Math.random())*1000);
    var EvalCaller=new Delegate.EvalCaller(delegate,args,cid);
    Delegate.Coll[cid]=EvalCaller;
    return cid;
    }
    function Delegate.Coll.Remove(cid)
    {
    delete Delegate.Coll[cid];
    Delegate.Coll.Length--;
    }
      

  5.   

    好复杂,我想实现
    oNewTd.attachEvent("onmousemove",cell_onMouseMove(this));
    该怎样调用?
      

  6.   

    很简单,你看看MSDN的例子嘛。<PUBLIC:ATTACH EVENT="ondetach" ONEVENT="cleanup()" /><SCRIPT LANGUAGE="JScript">
    attachEvent ('onmouseover', Hilite);
    attachEvent ('onmouseout', Restore);function cleanup()
    {
       detachEvent ('onmouseover', Hilite);
       detachEvent ('onmouseout', Restore);
    }function Hilite()
    {
       if (event.srcElement == element)
       { 
         normalColor = style.color;  
         runtimeStyle.color  = "red";
         runtimeStyle.cursor = "hand";
       }
    }function Restore()
    {
       if (event.srcElement == element)
       {
          runtimeStyle.color  = normalColor;
          runtimeStyle.cursor = "";
       }
    }
    </SCRIPT>
      

  7.   

    忘了告诉你。
    事件处理函数的第一个参数就是event.
    所以如果要放其他的,要放在第二个参数。
      

  8.   

    >好复杂,我想实现
    >oNewTd.attachEvent("onmousemove",cell_onMouseMove(this));
    >该怎样调用?
    --------------
    你把我的那脚本(-----下面的)放成一个文件引用。
    然后这样:
    oNewTd.attachEvent("onmousemove",Delegate(new Object(),cell_onMouseMove).PreInvoke(0,this));
    那么
    function cell_onMouseMove(eventObj,theThis)
    {
    ..
    }
    里面eventObj就是event,theThis就是刚才的this
    如果:
    oNewTd.attachEvent("onmousemove",Delegate(this,cell_onMouseMove);
    //this必须是JScript生成的对象
    那么:
    function cell_onMouseMove()
    {
    ...
    }
    里面的可以直接用this就是刚才传来的this了。
      

  9.   

    直接用input1.attachEvent("onmouseout",fnXX)然后载fnXX中用event.srcElment代替this如果要参数,可以用input1.attachEvent("onmouseout",new Function("fnXX(this)"))
      

  10.   

    实际上不是那么简单的。
    event没有提供attachElement
    就是说
    <div><span><b><a>,dskfj</a></b></span></div>
    div.attachEvent.....
    当a被点击的时候,
    event.srcElement是a不是div
    当然可以event.srcElement.parentElement....循环去找。
    不过如果是多层的<div>那么就惨了:
    <div>....<div>....<a>adasfd</a>...</div>...</div>
    两个div都关联了事件。
    要处理哪个div都不知道啊。
      

  11.   

    qiushuiwuhen(秋水无恨):
    -------------------------------------
    你的那个
    new Function
    是不能用的。
    因为里面的是字符串,而不是真正对象的直接引用。
    要么就搞个全局的hashtable,然后每个对象都有个字符串名字,那就能用了。
    实际上我的哪个Delegate就是全局的Coll
    本来function是有apply的。但是需要JScript5.5所以没有办法了。
      

  12.   

    如果是在BEHAVIOR下就省点.直接element就是了.
      

  13.   

    srcElement + this(或者htc里的element) 就解决了,哪里来那么复杂?实际上根据DOM,应该是target和currentTarget,用mozilla就很清楚了。
      

  14.   

    多谢lostinet(Lostinet),你的办法很有效,我们已经用上了。我前面没说清楚,其实我要实现的是
    oNewTd.attachEvent("onmousemove",cell_onMouseMove(oNewTd));
    误导大家了,不好意思。如果只是想实现上面的效果,有没有简单一点的?