我以前問過這個問題,但從來沒有真正得到它的工作。全局變量jquery
或多或少,我有多個JS文件,我需要檢查對一個變量。我有一個函數,檢查是否存在一個類,然後更改該變量。
var states = function(){
if($('#Animation.switch.switchOff').length == 1){
animationtoggle = 0;
}
else {
animationToggle = 1 ;
}
if($('#Autoscroll.switch.switchOff').length == 1){
scrolltoggle = 1;
}
else {
scrolltoggle = 0
}
return {
animationtoggle: animationtoggle,
scrolltoggle: scrolltoggle
};
}
然後我有另一個函數內,在另一個JS文件這樣的:
Okay, I've done this, although It still doesn't fix my problem of being able to use this variable on another file, I have this inside the function:
$(function() {
var ss = states();
ss.animationtoggle;
var last_position = 0;
var duration = 300;
if (animationtoggle == 1){
//only do something if it's equal to 1 "on"
});
這是切換按鈕代碼:
$('#optionMenu ul li').click(
function() {
$(this).toggleClass('switchOff');
var ss = states();
ss.scrolltoggle;
ss.animationtoggle;
});
上午我處理這個正確的方式?
請幫忙!這裏是我的地盤,切換到了右上角的那一刻:http://shannonhochkins.v5.cloudsvr.com.au/
這是要成爲全球訪問的問題的變量?酷站點btw。 – Jeemusu 2012-08-14 02:12:02
都scrolltoggle和animationtoggle @Jeemusu – Shannon 2012-08-14 02:16:02
所以你有什麼問題,你的變量不改變正確,其實你已經宣佈VAR animationtoggle = 1,如果你希望它是全球性的聲明它 - > animationtoggle = 1, – 2012-08-14 02:25:51