Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/454.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 对全局变量不更新感到困惑_Javascript_Jquery - Fatal编程技术网

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

Javascript 对全局变量不更新感到困惑,javascript,jquery,Javascript,Jquery,我有以下代码: 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;

我有以下代码:

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); 
var clickCount=1;
var slideCount=$('div.slide')。长度;
$('a#下一步按钮')。单击(函数(){
如果(单击计数<滑动计数){
$('div.slide')。设置动画({“left”:“-=”+slideWidth},'slow');
clickCount=clickCount+1;
}
});
$('p')。文本(单击计数);
它有一个名为
clickCount
的全局变量

$('a#next button')。每次单击
元素时,单击(function(){…
以1的增量更新全局变量

所以,我的问题是,为什么:
$('p').text(clickCount);

每次单击
标记时,不会在页面上显示更新的clickCount。相反,它只显示1(原始赋值)。

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

要解决此问题,请将
$('p')。文本(单击计数);
放在
单击功能中

$('a#next-button').click(function() {
  if(clickCount < slideCount) {
     $('div.slide').animate({"left":"-=" + slideWidth}, 'slow');
     clickCount = clickCount + 1;
     $('p').text(clickCount); 
  }
});
$('a#下一步按钮')。单击(函数(){
如果(单击计数<滑动计数){
$('div.slide')。设置动画({“left”:“-=”+slideWidth},'slow');
clickCount=clickCount+1;
$('p')。文本(单击计数);
}
});

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

要解决此问题,请将
$('p')。文本(单击计数);
放在
单击功能中

$('a#next-button').click(function() {
  if(clickCount < slideCount) {
     $('div.slide').animate({"left":"-=" + slideWidth}, 'slow');
     clickCount = clickCount + 1;
     $('p').text(clickCount); 
  }
});
$('a#下一步按钮')。单击(函数(){
如果(单击计数<滑动计数){
$('div.slide')。设置动画({“left”:“-=”+slideWidth},'slow');
clickCount=clickCount+1;
$('p')。文本(单击计数);
}
});

每次变量更改时,您都需要更新段落文本。段落不会神奇地跟踪clickCount变量的值:

$('a#next-button').click(function() {
    if(clickCount < slideCount) {
        $('div.slide').animate({"left":"-=" + slideWidth}, 'slow');
        clickCount = clickCount + 1;
        $('p').text(clickCount); 
    }
});
$('a#下一步按钮')。单击(函数(){
如果(单击计数<滑动计数){
$('div.slide')。设置动画({“left”:“-=”+slideWidth},'slow');
clickCount=clickCount+1;
$('p')。文本(单击计数);
}
});

每次变量更改时,您都需要更新段落文本。段落不会神奇地跟踪clickCount变量的值:

$('a#next-button').click(function() {
    if(clickCount < slideCount) {
        $('div.slide').animate({"left":"-=" + slideWidth}, 'slow');
        clickCount = clickCount + 1;
        $('p').text(clickCount); 
    }
});
$('a#下一步按钮')。单击(函数(){
如果(单击计数<滑动计数){
$('div.slide')。设置动画({“left”:“-=”+slideWidth},'slow');
clickCount=clickCount+1;
$('p')。文本(单击计数);
}
});

首先,如果要增加1,可以使用
clickCount++
,如果要增加2,则可以使用
clickCount+=2,依此类推。其次,
未定义滑动宽度(第6行)。谢谢你的回复。'SlideWidth'在实际代码中定义。首先,如果你想增加1,你可以使用
clickCount++
,如果增加2,你可以使用
clickCount+=2,依此类推。其次,
SlideWidth
没有定义(第6行)。谢谢你的回复。'SlideWidth'在实际代码中定义。