[英]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.