insertAdjacentHTML()很好用,可以不用attachEvent阿
解决方案 »
- js换行
- 紧急求救:如何使用jquery在页面中返回本地文件:a.json
- 如何实现树形结构与单击事件的配合显示?
- 请问javascript里有没有类似VBSCRIPT中InStr的函数
- 怎样实现那种点击一个链接弹出一个固定大小的窗口???
- 实现一个倒计时不应刷新就重新计时,倒计时程序我已编好!郁闷了一天,各位帮个忙!先谢啦!
- 菜鸟跪问一个简单的javaSript的问题
- 关于用javascript实现获取checkbox值得功能!50分!在线等!
- 如何保护javascript代码?
- 那位大虾指点一下如何制作带有滚动条的表格!谢谢、、、、、
- 怎样才能使一段含有字符,汉字,标点的文字在网页显示时能够撑满一行,行行对齐呢?
- form 提交的问题.
其实我想传的主要就是this,有没有办法获取这个事件绑定的对象(不是srcElement)?
比如说
<table>
<tr>
<td onclick="fn()">
<table>....<input>...
</table>
</td>
<td onclick="fn()"></td>
</tr>
</table>要是有人点了input,我在fn()中怎样处理?我的tr,td以及里面的html元素都是用程序生成的。
不过在低版本的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--;
}
oNewTd.attachEvent("onmousemove",cell_onMouseMove(this));
该怎样调用?
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>
事件处理函数的第一个参数就是event.
所以如果要放其他的,要放在第二个参数。
>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了。
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都不知道啊。
-------------------------------------
你的那个
new Function
是不能用的。
因为里面的是字符串,而不是真正对象的直接引用。
要么就搞个全局的hashtable,然后每个对象都有个字符串名字,那就能用了。
实际上我的哪个Delegate就是全局的Coll
本来function是有apply的。但是需要JScript5.5所以没有办法了。
oNewTd.attachEvent("onmousemove",cell_onMouseMove(oNewTd));
误导大家了,不好意思。如果只是想实现上面的效果,有没有简单一点的?