我正在使用.each()方法和$(this)选择器填充带有元素的数组。
(function($){
var elements = new Array();
var index = 0;
$("img").each(function() {
if($(this).attr("attribute")){
$(this).attr("imageIndex", index);
elements[index] = $(this);
index++;
}
});
}(jQuery));
Run Code Online (Sandbox Code Playgroud)
我想向单击该数组中任何元素时执行的代码添加事件侦听器。
例如:
$(elements).click = function(){
console.log("success");
}
Run Code Online (Sandbox Code Playgroud)
我认为onclick属性可以在循环遍历每个图像时进行更改,但这似乎有些不简洁。我想确定那是我实施之前的不得已的方法。