gpt4 book ai didi

javascript - 对全局变量不更新感到困惑

转载 作者:行者123 更新时间:2023-11-30 10:51:36 25 4
gpt4 key购买 nike

我有以下代码:

var clickCount = 1;
var slideCount = $('div.slide').length;

$('a#next-button').click(function() {
if(clickCount < slideCount) {
$('div.slide').animate({"left":"-=" + slideWidth}, 'slow');
clickCount = clickCount + 1;
}
});

$('p').text(clickCount);

它有一个名为 clickCount 的全局变量.

$('a#next-button').click(function() { …每次 <a> 时,以 1 为增量更新全局变量元素被点击。

所以,我的问题是,为什么:$('p').text(clickCount);<a> 期间不向我显示页面上更新的 clickCount标签被点击。相反,它只显示 1(原始分配值)。

最佳答案

变量正在更新,但您的函数 ($('p').text(clickCount);) 仅运行一次,因此仅使用它看到的值。

要解决此问题,请将 $('p').text(clickCount); 放在 click 函数中。

$('a#next-button').click(function() {
if(clickCount < slideCount) {
$('div.slide').animate({"left":"-=" + slideWidth}, 'slow');
clickCount = clickCount + 1;
$('p').text(clickCount);
}
});

关于javascript - 对全局变量不更新感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4983608/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com