[英]Why does this jquery each() loop doesn't operate on unique classes
I'm trying to make a jquery function work on classes with a unique identifier.. But it seems like it isn't adding the var to the element... How do I do this 我正在尝试使jquery函数在具有唯一标识符的类上工作。但是,似乎没有将var添加到元素中。
My code is this... 我的代码是这个...
var x = 1; $('.newest_posts').each(function() { $('.showFull' + x).on('click', function(e) { $('.newest_small' + x).hide(); // hide image preview on delete click $('.newest_full' + x).show(); // hide image delete link on click e.preventDefault(); }); x = x + 1; });
Not very well versed in jquery but i'm stumbled because I have this code which works for hiding them first 不太精通jQuery,但是我迷迷糊糊,因为我有这段代码可以首先隐藏它们
var i = 1; $('.newest_posts').each(function() { $('.newest_full' + i).hide(); i = i + 1; });
You have to ask the question: what does x
equal when the click handler is called ? 您必须提出一个问题: 单击处理程序被调用时 x
等于多少? It will not be equal to the x
value when you registered the handler, but be equal to the value of x
after the loop has completed. 注册处理程序时,它不等于x
值,但是在循环完成后等于x
的值。
Assuming nothing else modified it, it will be equal to $(.newestPosts).length() + 2
. 假设没有其他修改,它将等于$(.newestPosts).length() + 2
。
Here is the quick fix for the problem: 这是解决该问题的快速方法:
var x = 1;
$('.newest_posts').each(function() {
var xCopy = x;
$('.showFull' + xCopy).on('click', function(e) {
$('.newest_small' + xCopy).hide(); // hide image preview on delete click
$('.newest_full' + xCopy).show(); // hide image delete link on click
e.preventDefault();
});
x = x + 1;
});
It creates a new variable inside the scope of the handling function. 它在处理函数的范围内创建一个新变量。
An even better fix is to dispense with the outer x
and use the first parameter of the each
handler, which will be the index. 更好的解决方法是省去外部x
并使用each
处理程序的第一个参数,该参数将成为索引。
$('.newest_posts').each(function(index) {
var x = index + 1;
$('.showFull' + x).on('click', function(e) {
$('.newest_small' + x).hide(); // hide image preview on delete click
$('.newest_full' + x).show(); // hide image delete link on click
e.preventDefault();
});
});
You must first understand that javascript is asynchronous, and that code doesn't run the way you'd expect it to run in a synchronous programming language. 您必须首先了解javascript是异步的,并且代码不会以您期望它在同步编程语言中运行的方式运行。
The each loop runs when the page loads, incrementing the x variable, whereas the function you passed as a parameter to the 'click' event only runs when the actual element is clicked on the page. 每个循环在页面加载时运行,递增x变量,而作为参数传递给'click'事件的函数仅在页面上单击实际元素时运行。 At that point, the value of your x is going to be the same as the number of posts that you have. 届时,x的值将与您拥有的帖子数相同。
You could do it like this: 您可以这样做:
var x = 1;
$('.newest_posts').each(function() {
$('.showFull' + x).on('click', function(e) {
var thisElementsIndex = $(this).attr("class").split(' ').filter(function(el){
return el.indexOf('showFull') != -1
})[0].replace('showFull', '');
$('.newest_small' + thisElementsIndex ).hide(); // hide image preview on delete click
$('.newest_full' + thisElementsIndex ).show(); // hide image delete link on click
e.preventDefault();
});
x = x + 1;
});
What happens here is, the 'each' iteration using the x variable makes sure that all of your classes trigger that callback when you click on them. 这里发生的是,使用x变量进行“每个”迭代可确保您的所有类在单击它们时都触发该回调。 In the callback, you retrieve the value for x from the class name and pass it to the hide/show selectors. 在回调中,您从类名称中检索x的值,并将其传递给隐藏/显示选择器。
You can also use the data()
method to store its index in order to use it inside the element's onclick function: 您还可以使用data()
方法存储其索引,以便在元素的onclick函数中使用它:
var x = 1;
$('.newest_posts').each(function() {
$('.showFull' + x).data('index', x);
$('.showFull' + x).on('click', function(e) {
var index = $(this).data('index');
$('.newest_small' + index).hide(); // hide image preview on delete click
$('.newest_full' + index).show(); // hide image delete link on click
e.preventDefault();
});
x = x + 1;
});
This have the advantage that maps each iteration index to its corresponding element, allowing other functions to benefit from it; 这具有将每个迭代索引映射到其对应元素的优点,从而允许其他函数从中受益。 also you don't have to care about the value of x, whose scope is out of the function. 同样,您也不必关心x的值,x的范围不在函数范围之内。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.