$('.myElem').live('click', function() {
$(this).hide(500, function() {
$(this).siblings('.myOtherElem').show();
});
});
以上不起作用,因为$(this)
回调中不再处于正确的范围内。如何将我的原始源元素传递给回调?
$('.myElem').live('click', function() {
$(this).hide(500, function() {
$(this).siblings('.myOtherElem').show();
});
});
以上不起作用,因为$(this)
回调中不再处于正确的范围内。如何将我的原始源元素传递给回调?
实际上你的代码应该可以工作。
要this
在内部 javascript 方法中访问,您可以将引用存储在外部方法范围中:
$('.myElem').on('click', function() {
var myElem = this;
$(this).hide(500, function() {
$(myElem).siblings('.myOtherElem').show();
});
});
然而,在大多数 jQuery 方法this
中,是指使用的选择器或元素:
$('.myElem').on('click', function() {
// This refers to the clicked element
$(this).hide(500, function() {
// This refers to the clicked element as well
$(this).siblings('.myOtherElem').show();
});
});
$('.myElem').live('click', function() {
var $this = $(this);
$this.hide(500, function() {
$this.siblings('.myOtherElem').show();
});
});
$('.myElem').live('click', function() {
$(this).hide(500);
$(this).siblings('.myOtherElem').show();
});