Мой код выглядит так:
$(".div-text").click(function(e) {
e.preventDefault();
$(this).parents(".items").find("img.mainImage").each(function() {
// following this should correspond to mainImage elements and not outside "this"
alert($(this).data("src")) ----------> **this "this" does not work**
})
});
Как я могу использовать два элемента, соответствующие различным элементам или
Какова альтернатива для кода выше???
this
внутри each
цикла относится к текущему mainImage
который зацикливается через не щелкнув элемент div-text
.
Возможным решением является использование переменной закрытия, как показано ниже
$(".div-text").click(function (e) {
e.preventDefault();
var self = this;
$(this).parents(".items").find("img.mainImage").each(function () {
// following this should correspond to mainImage elements and not outside "this"
alert($(self).data("src"))
})
});