attachEvent和arguments [英] attachEvent and arguments
问题描述
嗨!
我遇到了attachEvent函数的问题。我想动态地将
attachEvent添加到某些对象中,这样每个对象都可以执行具有不同参数值的
事件函数。问题是:如何将
将参数传递给事件函数?
以下代码显然不起作用,我应该如何修改它?
让它起作用吗?
for(var a = 0; a< length; a ++)
{
var id =" item_" + a;
var obj = __ document.getElementById(id);
obj.attachEvent(" onclick",eventClick);
}
函数eventClick()
{
alert(obj.value);
}
不幸的是,代码如下:obj.attachEvent(" onclick",
eventClick(any_param));是无效的。
我在某处执行了obj.attachEvent(" onclick",eventClick);
自动将obj传递给eventClick函数 - 这将是一个
回答我的问题,但是如何在函数内部使用obj?
''这个。''在这里不起作用..
有什么想法吗?
提前谢谢,
迎接,Lukasz
Hi!
I''m having a problem with attachEvent function. I''d like to add
attachEvent dynamically to some objects so that each could execute
event function with different parameter value. The question is: how to
pass the parameter to event function?
The following code obviously doesn''t work, how should i modify it to
get it to work?
for(var a=0;a<length;a++)
{
var id="item_"+a;
var obj =__document.getElementById(id);
obj.attachEvent("onclick", eventClick);
}
function eventClick()
{
alert (obj.value);
}
Unfortunately, code like this: obj.attachEvent("onclick",
eventClick(any_param)); is invalid.
I read somewhere that executing obj.attachEvent("onclick", eventClick);
automatically passes obj to eventClick function - that would be an
answer to my question but how can I use the obj inside the function?
''this.'' doesn''t work here..
Any ideas?
thanks in advance,
greets, Lukasz
推荐答案
for(var a = 0; a< length; a ++)
for(var a=0;a<length;a++)
{
var id = " item_" + a;
var obj = __ document.getElementById(id);
obj.attachEvent(" onclick",eventClick);
}
函数eventClick()
{
alert(obj.value);
}
不幸的是,代码如下:obj.attachEvent(" onclick",
eventClick(any_param));是无效的。
我在某处执行了obj.attachEvent(" onclick",eventClick);
自动将obj传递给eventClick函数 - 这将是一个
回答我的问题,但我如何在函数内部使用obj?
{
var id="item_"+a;
var obj =__document.getElementById(id);
obj.attachEvent("onclick", eventClick);
}
function eventClick()
{
alert (obj.value);
}
Unfortunately, code like this: obj.attachEvent("onclick",
eventClick(any_param)); is invalid.
I read somewhere that executing obj.attachEvent("onclick", eventClick);
automatically passes obj to eventClick function - that would be an
answer to my question but how can I use the obj inside the function?
你在哪里读到的?
我没有在我的Mac上启动IE所以这不是'没有测试,但我认为
以下应该有效。
for(var a = 0; a< length; a ++){
var id =" item_" + a;
var obj = document.getElementById(id);
obj.attachEvent(" onclick",function(){eventClick.call(obj)}) ;
}
函数eventClick(){
alert(this.value);
}
彼得
Where did you read that?
I don''t have IE fired up on my mac so this isn''t tested but I think the
following should work.
for (var a=0; a<length; a++) {
var id = "item_" + a;
var obj = document.getElementById(id);
obj.attachEvent("onclick", function(){eventClick.call(obj)});
}
function eventClick() {
alert (this.value);
}
Peter
petermich ... @ gmail.com写道:
petermich...@gmail.com wrote:
for(var a = 0; a< length; a ++)
{
var id =" item_" + a;
var obj = __ document.getElementById(id);
obj.attachEvent(" onclick",eventClick);
}
函数eventClick()
{
alert(obj.value);
}
不幸的是,代码如下:obj.attachEvent(" onclick",
eventClick(any_param));是无效的。
我在某处执行了obj.attachEvent(" onclick",eventClick);
自动将obj传递给eventClick函数 - 这将是一个
回答我的问题,但我如何在函数内部使用obj?
for(var a=0;a<length;a++)
{
var id="item_"+a;
var obj =__document.getElementById(id);
obj.attachEvent("onclick", eventClick);
}
function eventClick()
{
alert (obj.value);
}
Unfortunately, code like this: obj.attachEvent("onclick",
eventClick(any_param)); is invalid.
I read somewhere that executing obj.attachEvent("onclick", eventClick);
automatically passes obj to eventClick function - that would be an
answer to my question but how can I use the obj inside the function?
你在哪里读到的?
我没有在我的Mac上启动IE所以这不是'没有测试,但我认为
以下应该可以工作。
for(var a = 0; a< length; a ++){
var id =" item_" + a;
var obj = document.getElementById(id);
obj.attachEvent(" onclick",function(){eventClick.call(obj)}) ;
}
函数eventClick(){
alert(this.value);
}
Where did you read that?
I don''t have IE fired up on my mac so this isn''t tested but I think the
following should work.
for (var a=0; a<length; a++) {
var id = "item_" + a;
var obj = document.getElementById(id);
obj.attachEvent("onclick", function(){eventClick.call(obj)});
}
function eventClick() {
alert (this.value);
}
不,它不会。
有理由期待它会,但由于使用
attachEvent以及IE'的事件模型在这个
方面被打破的事实,该函数的运算符将引用全局对象,
不是DOM对象。
对于OP,试试这个帖子:
< URL:
< a rel =nofollowhref =http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/915a86b7beb5903/6547f33093165e12?lnk=gst&q=attachevent+addeventlistener&rnum=2#6547f33093165e12 target =_ blank> http://groups.google.com/group/comp=\"47f33093165e12
>
>
只有在附加多个
事件处理程序的情况下才应使用attachEvent。如果您只附加一个,并且想要将
引用返回到它所附加的对象,最好的方法是:
obj。 onclick = eventClick;
注意函数引用后面没有''()''。当obj'的click事件调用
eventClick时,它的运算符会将
引用到obj:
函数eventClick (){
var obj = this;
if(obj.nodeName){
alert(obj.nodeName);
}否则{
alert(obj);
}
}
-
Rob。
attachEvent should only be used where you are attaching more than one
event handler. If you are only attaching one, and you want to get a
reference back to the object it is attached to, the best way is:
obj.onclick = eventClick;
Note there is no ''()'' following the function reference. When
eventClick is called by obj''s click event, its this operator will refer
to obj:
function eventClick(){
var obj = this;
if (obj.nodeName) {
alert(obj.nodeName);
} else {
alert(obj);
}
}
--
Rob.
pe ********** @ gmail.com 写道:
>
你在哪里读到的?
>
Where did you read that?
我现在找不到网站,也许我只是误解了。
I can''t find the website now, maybe I just misunderstood.
我不喜欢没有IE在我的Mac上启动所以这没有经过测试但我认为
以下应该可以工作。
for(var a = 0 ; a< length; a ++){
var id =" item_" + a;
var obj = document.getElementById(id);
obj.attachEvent(" onclick",function(){eventClick.call(obj)}) ;
}
函数eventClick(){
alert(this.value);
}
I don''t have IE fired up on my mac so this isn''t tested but I think the
following should work.
for (var a=0; a<length; a++) {
var id = "item_" + a;
var obj = document.getElementById(id);
obj.attachEvent("onclick", function(){eventClick.call(obj)});
}
function eventClick() {
alert (this.value);
}
好吧,不幸的是,根据通过引用传递obj,函数
eventClick始终警告最后传递的对象的值,无论是
我点击的对象。
是否有可能按值传递参数,而不是参考,
Javascript?
谢谢,Lukasz
Well, unfortunately, according to passing obj by reference, function
eventClick always alerts the value of the last passed object, no matter
which object I click.
Is there a possibility to pass argument by value, not by reference, in
Javascript?
thanks, Lukasz
这篇关于attachEvent和arguments的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!