我有这个:
$('#slider li').click(function () {
var stepClicked = $(this).index();
alert(stepClicked);
if (stepClicked != 0) {
$('#cs_previous').removeClass('cs_hideMe');
} else {
$('#cs_previous').addClass('cs_hideMe');
}
$('li.cs_current').removeClass('cs_current');
$($(this)).addClass('cs_current');
moveToNextImage(stepClicked);
function moveToNextImage(stepClicked) {
alert(stepClicked);
var currentIs = $('li.cs_current').index();
var newLeftEdge = currentIs - stepClicked;
$('.cs_riskStageImage').fadeTo(200, .2).animate({
left: newLeftEdge
}, "fast").fadeTo(200, 1);
};
});
警报显示 li 单击的正确索引,当我在我调用的最后一个函数 moveToNextImage(stepClicked)
中提醒变量时,显示相同的值,但动画不是没有发生。这适用于许多其他方式,但我试图传递单击的列表项的索引值以用于数学计算。
..或者我可以将第一个函数中的值转换为另一个变量,然后传递给第二个函数吗?
最佳答案
javascript 函数 call()
和 apply()
都是为了在上下文中调用函数。
function sum() {
return this.num1 + this.num2;
}
function callSum(num1, num2) {
this.num1 = num1
this.num2 = num2
return sum.call(this); //call sum() in the context of this
}
alert(callSum(10, 15));
function applySum(num1, num2) {
this.num1 = num1
this.num2 = num2
return sum.apply(this); //call sum() in the context of this
}
alert(applySum(30, 45));
现在在 sum()
函数中,this
关键字与在 callSum()
和 中具有相同的上下文applySum()
函数。
call()
和apply()
的区别在于,apply 的第二个参数要么是要传递的参数数组,要么是一个arguments
对象。
关于javascript - 我可以将 "this"作为参数传递给 javascript 中的另一个函数吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9812445/