变量name
没有传递到这些函数中。如何将这些变量传递给这些函数?我还尝试了完全不使用局部变量name
,而只使用了全局变量arrayOfExpansions
,这也失败了。谢谢参观。
var arrayOfExpansions = ["a", "b", "c", "d", "e", "f", "g"];
$(document).ready(
function () {
for (var int = 0; int < arrayOfExpansions.length; int++) {
var name = arrayOfExpansions[int].toLowerCase();
console.log(name + "4");
$(function (name) {
console.log(name + "3");
$("#" + name + "Slider").slider({
range: true,
min: 0,
max: 10,
values: [0, 10],
slide: function (event, ui, name) {
console.log(name + "2");
$("#" + name + "SliderValue").html(ui.values[0] + " - " + ui.values[1]);
}
});
console.log(name + "1");
$("#" + name + "SliderValue").html($("#" + name + "Slider").slider("values", 0) + " - " + $("#" + name + "Slider").slider("values", 1));
});
}
});
我的
console.log()
的输出如下所示:a4
CardClass.js: 98b4
CardClass.js: 98c4
CardClass.js: 98d4
CardClass.js: 98e4
CardClass.js: 98f4
CardClass.js: 98g4
CardClass.js: 100function(selector, context) {
// The jQuery object is actually just the init constructor 'enhanced'
return new jQuery.fn.init(selector, context, rootjQuery);
}
3
CardClass.js: 111function(selector, context) {
// The jQuery object is actually just the init constructor 'enhanced'
return new jQuery.fn.init(selector, context, rootjQuery);
}
1
CardClass.js: 100function(selector, context) {
// The jQuery object is actually just the init constructor 'enhanced'
return new jQuery.fn.init(selector, context, rootjQuery);
}
3
CardClass.js: 111function(selector, context) {
// The jQuery object is actually just the init constructor 'enhanced'
return new jQuery.fn.init(selector, context, rootjQuery);
}
1
CardClass.js: 100function(selector, context) {
// The jQuery object is actually just the init constructor 'enhanced'
return new jQuery.fn.init(selector, context, rootjQuery);
}
3
CardClass.js: 111function(selector, context) {
// The jQuery object is actually just the init constructor 'enhanced'
return new jQuery.fn.init(selector, context, rootjQuery);
}
1
CardClass.js: 100function(selector, context) {
// The jQuery object is actually just the init constructor 'enhanced'
return new jQuery.fn.init(selector, context, rootjQuery);
}
3
CardClass.js: 111function(selector, context) {
// The jQuery object is actually just the init constructor 'enhanced'
return new jQuery.fn.init(selector, context, rootjQuery);
}
1
CardClass.js: 100function(selector, context) {
// The jQuery object is actually just the init constructor 'enhanced'
return new jQuery.fn.init(selector, context, rootjQuery);
}
3
CardClass.js: 111function(selector, context) {
// The jQuery object is actually just the init constructor 'enhanced'
return new jQuery.fn.init(selector, context, rootjQuery);
}
1
CardClass.js: 100function(selector, context) {
// The jQuery object is actually just the init constructor 'enhanced'
return new jQuery.fn.init(selector, context, rootjQuery);
}
3
CardClass.js: 111function(selector, context) {
// The jQuery object is actually just the init constructor 'enhanced'
return new jQuery.fn.init(selector, context, rootjQuery);
}
1
CardClass.js: 100function(selector, context) {
// The jQuery object is actually just the init constructor 'enhanced'
return new jQuery.fn.init(selector, context, rootjQuery);
}
3
CardClass.js: 111function(selector, context) {
// The jQuery object is actually just the init constructor 'enhanced'
return new jQuery.fn.init(selector, context, rootjQuery);
}
1
最佳答案
您的外部“就绪”处理程序中间的那个位置:
$(function(name) {
那正在建立另一个“就绪”处理程序。这就是“ $(someFunction)”的作用;换句话说,它与“ $(document).ready(someFunction)”完全相同。
因此,这根本不会像您期望的那样做任何事情,并且与参数传递没有太大关系。
如果要使用某个参数调用函数,请声明一个函数:
function theDaleFunction(name) {
console.log(name + "3");
$("#" + name + "Slider").slider({
range: true,
min: 0,
max: 10,
values: [0, 10],
slide: function (event, ui, name) {
console.log(name + "2");
$("#" + name + "SliderValue").html(ui.values[0] + " - " + ui.values[1]);
}
});
console.log(name + "1");
$("#" + name + "SliderValue").html($("#" + name + "Slider").slider("values", 0) + " - " + $("#" + name + "Slider").slider("values", 1));
}
将其放置在某个位置,可能完全在“ for”循环之前,或者可能在“ ready”处理程序之外;取决于其他东西。然后在循环中调用它:
theDaleFunction( name );
关于javascript - 在将变量“传递”到函数中时遇到麻烦吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8468268/