這很可能是簡單的,但即時通訊掙扎一點是有可能做到像下面這樣的事情,因爲它沒有在第二個函數中提醒。在另一個函數中使用一個函數的變量 - 想想我錯過了什麼
function func1() {
var test = 5;
var testAgain = 8;
}
function func3() {
func1();
alert(test);
}
編輯:
謝謝大家對您的意見,但我似乎無法得到它的工作。
我下面就是我試圖讓這個在
工作似乎並不想從setImageReelWidth通過增值經銷商,只是提醒0時,它得到旋轉=功能()或$代碼(「.paging一」)。點擊(函數()。所以我點擊它提醒4(這是正確的),然後當它再次兩次運行它,我只是得到0
var divWidth = 0;
var slideTotal = 0;
var divReelWidth = 0;
function setImageReelWidth() {
var divWidth = $(".window").width();
var slideTotal = $(".slide").size();
var divReelWidth = divWidth * slideTotal;
alert(slideTotal);
//Adjust the image reel to its new size
$(".image_reel").css({ 'width': divReelWidth });
}
$(document).ready(function() {
//////////////////////////// INITAL SET UP /////////////////////////////////////////////
//Get size of images, how many there are, then determin the size of the image reel.
//var divWidth = $(".window").width();
//var slideTotal = $(".slide").size();
//var divReelWidth = divWidth * slideTotal;
//Adjust the image reel to its new size
//$(".image_reel").css({ 'width': divReelWidth });
//set the initial not active state
$('#prev').attr("class", "not_active");
//////////////////////////// SLIDER /////////////////////////////////////////////
//Paging + Slider Function
rotate = function() {
setImageReelWidth();
alert(slideTotal);
var triggerID = $slideNumber - 1; //Get number of times to slide
var image_reelPosition = triggerID * divWidth; //Determines the distance the image reel needs to slide
//sets the active on the next and prev
if ($slideNumber == 1) {
$('#prev').attr("class", "not_active")
$('#next').attr("class", "active")
}
else if ($slideNumber == slideTotal) {
$('#next').attr("class", "not_active");
$('#prev').attr("class", "active");
}
else {
$('#prev').attr("class", "active")
$('#next').attr("class", "active")
};
//Slider Animation
$(".image_reel").animate({
left: -image_reelPosition
}, 500);
};
//////////////////////////// SLIDER CALLS /////////////////////////////////////////////
//click on numbers
$(".paging a").click(function() {
setImageReelWidth();
alert(slideTotal);
setImageReelWidth();
$active = $(this); //Activate the clicked paging
$slideNumber = $active.attr("rel");
rotate(); //Trigger rotation immediately
return false; //Prevent browser jump to link anchor
});
我想你想編寫alert(test);'not'alert('test');',因爲使用String'test'的警報不使用你的變量。但是,這確實可以解決您的問題,請參閱下面的答案。 – 2012-02-07 10:16:37
在函數中使用'var'關鍵字時,它將創建一個新的局部變量,而不是使用全局變量。只要從函數內部刪除'var'字。 – JJJ 2012-02-07 11:36:19
完美多數民衆贊成在我做錯了 – odd 2012-02-07 12:14:31