有谁能够帮我 ?- >
我不会在 jQuery.each 之后定义函数内部的 vars id、zIndex、width,它们的值将是传递给函数 MyFunction 的数组中的值:
console.log(id); //ID IS NOT DEFINED
console.log(zIndex); //NOT DEFINED
console.log(width); //NOT DEFINED
非常感谢您的帮助,彼得
代码>
var MyArray = new Object();
MyArray.id = "3";
MyArray.zIndex = "3";
MyArray.width = "600";
function MyFunction(my_array) {
console.log(my_array.id); //IT IS OK, id is defined
jQuery.each(my_array, function(key,value) {
alert(key + " = " + value);
eval("var" + key + " = " + value); //???????
});
console.log(id); //ID IS NOT DEFINED
console.log(zIndex); //NOT DEFINED
console.log(width); //NOT DEFINED
}
MyFunction(MyArray);
- - -更新 - - - - - - - - - - -
感谢大家在我更新代码的同时回答所有问题,我想为我的应用程序方法创建小插件
OPENWINDOW 是公共的
我的问题是如何使用方法
jQuery.each(my_array, function(key,value) {
window[key]=value;
});
在插件公共方法打开窗口中
这是更复杂的代码:
(function($){
$.fn.MyPlugin = function(options) {
...... CODE
this.OpenWindow = function(my_array) {
//this way dont work for me here, vars are not defined
var window = $(window);
jQuery.each(my_array, function(key,value) {
window[key]=value;
});
//VARS ARE NOT DEFINED
console.log(id);
console.log(zIndex);
console.log(width);
//this way work well
for (var k in my_array){
eval ("var " + k + " = " + my_array[k]);
}
//VARS ARE DEFINED
console.log(id);
console.log(zIndex);
console.log(width);
.... code
return this;
};
})(jQuery);
var MyArray = new Object();
MyArray.id = "3";
MyArray.zIndex = "3";
MyArray.width = "600";
var MyPlugin = $.fn.MyPlugin();
MyPlugin.OpenWindow(MyArray);