使用带有for循环的jquery定义单击事件 [英] Defining click events using jquery with for loop
问题描述
以下是代码:
Here's the code:
Popups = [{c:'#Payment-Selection',w:'#Cards-Popup'},{c:'#Drop-SY',w:'#Start-Years'}];
var ActivatePopups = function() {
for (var i=0; i<Popups.length; i++) {
$(Popups[i]['c']).click(function() { PopupDrop(Popups[i]['w']); });
}
}
ActivatePopups();
实际Popups数组要长得多,这就是为什么我要使用这种方法。无论如何,这不起作用,因为当我定义函数粗体函数时会出现问题。
触发点击,但似乎触发功能未完成,因为我收到了这样的错误 - :
'未捕获的TypeError:无法读取未定义的属性'w''
Actual Popups array is much longer, thats why I want to use such method. Anyway, this does not work because there is problem when I define function bolded function.
The click is triggered, but it seems, that the firing function is not complete, because I'm getting such error¬:
'Uncaught TypeError: Cannot read property 'w' of undefined'
推荐答案
(弹出窗口[ i] [' c'])。click(function(){PopupDrop(Popups [i] [ ' w']);});
}
}
ActivatePopups();
(Popups[i]['c']).click(function() { PopupDrop(Popups[i]['w']); }); } } ActivatePopups();
实际Popups数组要长得多,这就是为什么我要使用这种方法。无论如何,这不起作用,因为当我定义函数粗体函数时会出现问题。
触发点击,但似乎触发功能未完成,因为我收到了这样的错误 - :
'未捕获的TypeError:无法读取未定义的属性'w'
Actual Popups array is much longer, thats why I want to use such method. Anyway, this does not work because there is problem when I define function bolded function.
The click is triggered, but it seems, that the firing function is not complete, because I'm getting such error¬:
'Uncaught TypeError: Cannot read property 'w' of undefined'
如果没有你的内容很难说出错了什么代码PopupDrop
;但是让我们用更精细的细节来写它。也许你打算做一些不同的事情。让我们看看:
It hard to say what's wrong without having your code forPopupDrop
; but let's write it in finer detail. Maybe you meant to do something different. Let's see:
var ActivatePopups = function() {
for (var i=0; i<popups.length;>
cElement =
(Popups [i] .c); // id选择器找到的元素的jQuery包装器
// 另外,请注意使用索引'c'和'w'的更好语法
wElement =
(Popups[i].c); // jQuery wrapper for the element found by the id selector // also, pay attention for better syntax for using indices 'c' and 'w' wElement =
这篇关于使用带有for循环的jquery定义单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!