Javascript 函数中丢失变量
有人能告诉我为什么第一个Javascript 函数中丢失变量,javascript,Javascript,有人能告诉我为什么第一个警报(items.index($(this))=1和第二个警报(items.index($(this))=1。如何在其他函数中更改此值 $(function () { var items = $('#v-nav>ul>li').each(function () { $(this).click(function () { //remove previous class and add it to clicked tab i
警报(items.index($(this))=1
和第二个警报(items.index($(this))=1
。如何在其他函数中更改此值
$(function () {
var items = $('#v-nav>ul>li').each(function () {
$(this).click(function () {
//remove previous class and add it to clicked tab
items.removeClass('current');
$(this).addClass('current');
alert(items.index($(this)));
$('#v-nav>div.tab-content').fadeOut("slow", function () {
alert(items.index($(this)));
$('#v-nav>div.tab-content').eq(items.index($(this))).fadeIn("slow");
});
// window.location.hash = $(this).attr('tab');
});
});
此
指当前对象。
在第一个版本中
此
是$('#v-nav>ul>li')
列表中的一项
在第二个版本中
此
是由$('#v-nav>div.tab-content')选择的DOM对象。
如果要保留此的先前值,请将其缓存在变量中。 (缓存
$(这)
是一种很好的做法,因为您总是保存函数调用)
使用$(this)
时,实际上是将this
传递到$
函数中
$(function () {
var items = $('#v-nav>ul>li').each(function () {
var $this = $(this);
$this.click(function () {
//remove previous class and add it to clicked tab
items.removeClass('current');
$this.addClass('current');
alert(items.index($this));
$('#v-nav>div.tab-content').fadeOut("slow", function () {
alert(items.index($this));
$('#v-nav>div.tab-content').eq(items.index($(this))).fadeIn("slow");
});
// window.location.hash = $(this).attr('tab');
});
});
此
指当前对象。
在第一个版本中
此
是$('#v-nav>ul>li')
列表中的一项
在第二个版本中
此
是由$('#v-nav>div.tab-content')选择的DOM对象。
如果要保留此的先前值,请将其缓存在变量中。 (缓存
$(这)
是一种很好的做法,因为您总是保存函数调用)
使用$(this)
时,实际上是将this
传递到$
函数中
$(function () {
var items = $('#v-nav>ul>li').each(function () {
var $this = $(this);
$this.click(function () {
//remove previous class and add it to clicked tab
items.removeClass('current');
$this.addClass('current');
alert(items.index($this));
$('#v-nav>div.tab-content').fadeOut("slow", function () {
alert(items.index($this));
$('#v-nav>div.tab-content').eq(items.index($(this))).fadeIn("slow");
});
// window.location.hash = $(this).attr('tab');
});
});
<>你必须考虑每个“这个”的上下文,每个回调都有一个明显的“这个”变量。如果您想保留原始版本,请执行以下操作:
var self = this;
<>你必须考虑每个“这个”的上下文,每个回调都有一个明显的“这个”变量。如果您想保留原始版本,请执行以下操作:
var self = this;
在动画的回调函数中,
此
不是您单击的元素,而是正在设置动画的元素
回调未发送任何参数,但已设置为DOM
正在设置动画的元素。”
(如果未将其设置为动画元素,则它将是对窗口
对象的引用。)
将引用复制到动画调用外部的变量:
var t = this;
$('#v-nav>div.tab-content').fadeOut("slow", function () {
alert(items.index($(t)));
$('#v-nav>div.tab-content').eq(items.index($(t))).fadeIn("slow");
});
在动画的回调函数中,
此
不是您单击的元素,而是正在设置动画的元素
回调未发送任何参数,但已设置为DOM
正在设置动画的元素。”
(如果未将其设置为动画元素,则它将是对窗口
对象的引用。)
将引用复制到动画调用外部的变量:
var t = this;
$('#v-nav>div.tab-content').fadeOut("slow", function () {
alert(items.index($(t)));
$('#v-nav>div.tab-content').eq(items.index($(t))).fadeIn("slow");
});
就目前情况而言,我在问题中没有看到任何
return
语句。您还有什么要向我们透露的吗?在完成每个函数之前,您的items变量似乎不会被填充。虽然我不能不看代码就说出来。我已经重新措辞了这个问题。这可能会让我看到的行为更加清晰。就目前的情况来看,我在问题中没有看到一个return
语句。您还有什么要向我们透露的吗?在完成每个函数之前,您的items变量似乎不会被填充。虽然我不能不看代码就说出来。我已经重新措辞了这个问题。这可能会让我看到的行为更加清晰。