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)'
...
}
我在此函数中定义了一个全局变量,但它不起作用。如果我在它工作的函数中定义变量,但我希望它是全局的。
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)'
...
}