繁体   English   中英

jQuery函数中的全局变量不起作用

[英]global variable in jquery function doesn't work

我已经在此函数中定义了一个全局变量,但是它不起作用。 如果我在函数中定义变量,则它可以工作,但我希望它是全局的。

var currentpage = 1;

    $(document).ready(function(){

      function checkpage(currentpage) { 
        if (currentpage == 1) {
          $(".text_nav").html("some text");
        };
      }

      checkpage();

    });

我为什么要它? 因为我想减少点击变量以执行某些操作

$( "#button" ).click(function() {
  currentpage += 1;
});

//check currentpage variable again (who?)
            if (currentpage == 2) {
              $(".text_nav").html("some other text");
            };

它确实起作用,您没有向函数发送任何内容,请执行以下操作:

 checkpage(currentpage);

currentpage的功能是一个参数,它是该函数的局部变量,是从全局变量分开。 或者,如果不需要,只需删除该参数:

function checkpage() { // Other than 'checkpage(currentpage)'
   ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM