jQuery"this"链条式变化 [英] jQuery "this" changing in chain
问题描述
我有一个div,里面有一个div,当单击该div时,它将删除外部div.实现这一目标的jQuery是
I have a div that has a div inside it that will delete the outer div when clicked. The jQuery to make this happen is
275 $(document).on('click','.rightMenuDelete', function(event) {
276 $(this).parents('.cell').
277 fadeOut(300, function() {
278 $(this).remove();});
279 });
在276,此关键字是我们单击的内部div.到277时,已包装的集合具有我们要删除的外部div.但是我想在删除外部div之前淡出它,所以277应用了fadeOut,然后回调删除了外部div. 278使我惊讶的是. 278处的this关键字是我们需要的外部div,不再是我们最初单击的内部div.任何人都可以对当控件在jQuery链中向下移动时this关键字的更改方式有所了解吗?
At 276 the this keyword is the inner div we clicked. By 277 the wrapped set has the outer div we want to delete. But I want to fade out the outer div before I remove it, so 277 applies the fadeOut and then the callback removes the outer div. What surprises me a bit is that 278 works. The this keyword at 278 is the outer div, as we want , and no longer the inner div that we clicked originally. Can anyone throw some light on how the this keyword can change as control moves down a jQuery chain?
谢谢
推荐答案
当执行parents('.cell')
时,this
指向的元素在276行本身发生更改.它不再指向inner
div,而是指向具有cell
类的父级列表.
The element pointed to by this
changes on line 276 itself, when parents('.cell')
is executed. It no longer points to the inner
div, but to the list of parents with the class cell
.
之后,您将执行fadeOut,从而循环遍历父级集合.在循环内部,this
表示当前正在循环的父级.
After which you execute the fadeOut which loops over the collection of parents. Inside the loop, this
refers to the current parent being looped over.
这篇关于jQuery"this"链条式变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!