如何获得object通过attachEvent增加的函数?

jsidiot 2005-12-19 04:45:15
如题!
...全文
171 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
jsidiot 2005-12-20
  • 打赏
  • 举报
回复
呵呵,我贴这段代码不是说证明这段代码是用什么方式绑定run的
而是让大家看看test对象的属性变化
在动态attachEvent给对象的时候,有没有属性发生变化
至少从目前看来是没有变化的
meizz 2005-12-19
  • 打赏
  • 举报
回复
仔细看了一下秋水的代码,eval(tag+"."+evt+"=new Function('run()')");
这句代码不就等同于 eval(tag +"."+ evt +"=function(){run()}"); 吗?
还是直接赋函数的吗,不是 attachEvent
jsidiot 2005-12-19
  • 打赏
  • 举报
回复
引用:
嗯,我估计它应该是把对应的函数句柄赋给了对象的类似于 prototype 之类的一个扩展里了,所以直接调用是调用出来的。

如果照这么说,不是应该在属性里面也可以看出来呢?
这里有一段qiushuiwuhen的代码
倒是可以看到属性变化的,改改应该可以
但是咋的就看不出来呢?
<input id=test>
<textarea id=show ></textarea>
<script>
var tag="test",count=0;
for(evt in eval(tag)){
if(evt.substr(0,2)=="on")
eval(tag+"."+evt+"=new Function('run()')");
}
function run(){
str=(++count)+"."+tag+"的"+event.type+"事件\n"
for(obj in event)if(eval("event."+obj)){
str+=" " + obj+":"+eval("event."+obj)+"\n";
}
show.value=str+show.value;
}
</script>
dh20156 2005-12-19
  • 打赏
  • 举报
回复
”嗯,我估计它应该是把对应的函数句柄赋给了对象的类似于 prototype 之类的一个扩展里了,所以直接调用是调用出来的。“

同意梅大的看法!
dh20156 2005-12-19
  • 打赏
  • 举报
回复
这样试试:
<html>
<body >
<input id=test>
<input type=button onclick="addfun();" value=addfun >
<input type=button onclick="detachfun();" value=detachfun >
<input type=button onclick="getfocus();" value=getfocus >

</body>
</html>
<script>
function gg(){
alert("DD");
}

function addfun(){
//test.attachEvent("onfocus",function(){gg("ggg")});
test.onfocus = function(){gg("ggg")}
}
function detachfun(){
//test.detachEvent("onfocus",function(){gg()});
test.onfocus = null;
}
function getfocus(){
alert(test.onfocus);
}
</script>
jsidiot 2005-12-19
  • 打赏
  • 举报
回复
但是我如果通过这个页面把这些函数移植到其他的页面就困难了
至少我无法获得
meizz 2005-12-19
  • 打赏
  • 举报
回复
嗯,我估计它应该是把对应的函数句柄赋给了对象的类似于 prototype 之类的一个扩展里了,所以直接调用是调用出来的。
jsidiot 2005-12-19
  • 打赏
  • 举报
回复
我在猜想是不是在执行onfocus的时候,先去判断是否attachEvent的东西
或者内置了attachEvent所绑定函数的位置
要不就是绑定到window或者body里面
但是看了一下.好像body的属性都没有改变,
dh20156 2005-12-19
  • 打赏
  • 举报
回复
?
jsidiot 2005-12-19
  • 打赏
  • 举报
回复
看了一下,除了直接赋值
aa.onfocus = gg;
可以
其他情况都不行
jsidiot 2005-12-19
  • 打赏
  • 举报
回复
<html>
<body >
<input id=test >
<input type=button onclick="addfun();" value=addfun >
<input type=button onclick="detachfun();" value=detachfun >
<input type=button onclick="getfocus();" value=getfocus >

</body>
</html>
<script>
function gg(){
alert("DD");
}

function addfun(){
eval("test").attachEvent("onfocus",gg);
}
function detachfun(){
eval("test").detachEvent("onfocus",function(){gg()});
}
function getfocus(){
alert(test.onfocus);
}
</script>
meizz 2005-12-19
  • 打赏
  • 举报
回复
这个东东我也不会,不过象dh20156(风之石)的 obj.onclick 取到的只是 null 呀。
jsidiot 2005-12-19
  • 打赏
  • 举报
回复
TO:dh20156(风之石)
试过这种情况嘛?
<html>
<body >
<input id=test onfocus="alert();" >
<input type=button onclick="addfun();" value=addfun >
<input type=button onclick="detachfun();" value=detachfun >
<input type=button onclick="getfocus();" value=getfocus >

</body>
</html>
<script>
function gg(){
alert("DD");
}

function addfun(){
eval("test").attachEvent("onfocus",function(){gg("ddd")});
}
function detachfun(){
eval("test").detachEvent("onfocus",function(){gg()});
}
function getfocus(){
alert(test.onfocus);
}
</script>
创造奇迹9999 2005-12-19
  • 打赏
  • 举报
回复
想来凑个热闹,但我不会!

呵呵
dh20156 2005-12-19
  • 打赏
  • 举报
回复
要知道添加的是什么事件,比如onclick,onmouseover,你就可以直接这样获得:
var oldbh = obj.onclick;
jsidiot 2005-12-19
  • 打赏
  • 举报
回复
呵呵,
咋就我的问题就没有人回答呢?

87,915

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧