- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这很可能很简单,但我有点挣扎是否可以做下面这样的事情,因为它在第二个函数中没有提醒。
function func1() {
var test = 5;
var testAgain = 8;
}
function func3() {
func1();
alert(test);
}
编辑:
谢谢大家的评论,但我似乎无法让它工作。
我下面是我试图让它工作的代码
它似乎不想从 setImageReelWidth 传递 vars,当它到达 rotate = function () 或 $(".paging a").click(function () 时只提醒 0。所以我点击它警报 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
});
最佳答案
通过全局定义变量,使变量对两种方法都可用,例如 Rory建议 his answer ,很好。
另一种解决方案是从第一个函数返回它并在第二个函数中使用它,如下所示:
function func1() {
var test = 5;
return test;
}
function func3() {
var x = func1();
alert(x);
}
编辑另一种方法是编写一个工厂函数来创建 func3:
var func3 = (function(){
var test;
function func1() {
test = 5;
}
return function() {
func1();
alert(test);
}
})();
这里发生的事情如下:我创建了一个立即执行的函数:
(function(){
// ...
})();
在那里,我有变量 test
和另一个名为 func1
的函数,我返回一个绑定(bind)到变量 的函数
>函数3
。 func3
现在可以访问 var test
以及 func1
但在我编写的那个工厂函数之外什么都没有 可以访问它们。
查看我创建的 fiddle :enter link description here .
像这样的东西一开始有点难以理解,但这确实是 JavaScript 的强大之处(恕我直言)。
关于javascript - 在另一个函数中使用一个函数的变量——我想我遗漏了什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9174381/
使用登录后,我想吐出用户名。 但是,当我尝试单击登录按钮时, 它给了我力量。 我看着logcat,但是什么也没显示。 这种编码是在说。 它将根据我在登录屏幕中输入的名称来烘烤用户名。 不会有任何密码。
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎是题外话,因为它缺乏足够的信息来诊断问题。 更详细地描述您的问题或include a min
我是一名优秀的程序员,十分优秀!