Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript变量-需要一个变量_Javascript_Jquery_Variables_Variable Assignment - Fatal编程技术网

Javascript变量-需要一个变量

Javascript变量-需要一个变量,javascript,jquery,variables,variable-assignment,Javascript,Jquery,Variables,Variable Assignment,以下代码要求组合两个变量的名称 var myScroll; var orderit; var id; $('.scrollable').each(function(){ orderit += 1; $(this).attr('id', 'scrollp' + orderit); id = $(this).attr('id'); myscroll = new iScroll( id ); }); 所以问题是变量my scroll每次都必须是唯一的,所以它的

以下代码要求组合两个变量的名称

var myScroll; 
var orderit;
var id;   
$('.scrollable').each(function(){
    orderit += 1;
    $(this).attr('id', 'scrollp' + orderit);
    id = $(this).attr('id');
    myscroll = new iScroll( id );
});
所以问题是变量my scroll每次都必须是唯一的,所以它的名称必须是myscroll+orderit,但我不能这样命名变量

你知道怎么做吗


奇妙的

myScroll
做成一个数组,然后
push()

var myScroll = [];

var orderit;
var id;   
$('.scrollable').each(function(){
   orderit += 1;
   $(this).attr('id', 'scrollp' + orderit);
   id = $(this).attr('id');

   // push another iScroll() onto myScroll
   myScroll.push(new iScroll( id ));
 });

注意:在代码中,您声明了
myScroll
,但后来使用了
myScroll
。检查大小写差异-我在示例中修复了它。

myScroll
制作为一个数组,并将
push()

var myScroll = [];

var orderit;
var id;   
$('.scrollable').each(function(){
   orderit += 1;
   $(this).attr('id', 'scrollp' + orderit);
   id = $(this).attr('id');

   // push another iScroll() onto myScroll
   myScroll.push(new iScroll( id ));
 });

注意:在代码中,您声明了
myScroll
,但后来使用了
myScroll
。检查大小写差异-我在示例中修复了它。

尝试动态生成变量是错误的方法。可以这样做(虽然不是在“严格模式”下),但不建议这样做

相反,您应该创建一个集合。可以使用数组或对象

var orderit = 0;
var scrolls = {};
$('.scrollable').each(function(){
    this.id = 'scrollp' + (++orderit);
    scrolls[ 'myScroll' + orderit ] = new iScroll( this.id );
});

对于数组,可以使用
.map()
构建集合。然后使用
.toArray()
生成一个数组

var orderit = 0;
var myScroll = $('.scrollable').map(function(){
    this.id = 'scrollp' + (++orderit);
    return new iScroll( this.id );
}).toArray();
…并通过以下方式访问它们:

myScroll[0];
myScroll[1];
  // ...and so on
scrolls.myScroll1;
scrolls.myScroll2;
  // ...and so on

如果您想要
myScroll1、myScroll2
名称,则使用对象

var orderit = 0;
var scrolls = {};
$('.scrollable').each(function(){
    this.id = 'scrollp' + (++orderit);
    scrolls[ 'myScroll' + orderit ] = new iScroll( this.id );
});
…并通过以下方式访问它们:

myScroll[0];
myScroll[1];
  // ...and so on
scrolls.myScroll1;
scrolls.myScroll2;
  // ...and so on

仅供参考:不需要
.attr()
来获取和设置元素的ID。正如我在回答中所做的那样,只需使用
this.id
直接访问它



编辑:我使用了错误的值来构建对象属性。已修复。

尝试动态生成变量是错误的方法。可以这样做(虽然不是在“严格模式”下),但不建议这样做

相反,您应该创建一个集合。可以使用数组或对象

var orderit = 0;
var scrolls = {};
$('.scrollable').each(function(){
    this.id = 'scrollp' + (++orderit);
    scrolls[ 'myScroll' + orderit ] = new iScroll( this.id );
});

对于数组,可以使用
.map()
构建集合。然后使用
.toArray()
生成一个数组

var orderit = 0;
var myScroll = $('.scrollable').map(function(){
    this.id = 'scrollp' + (++orderit);
    return new iScroll( this.id );
}).toArray();
…并通过以下方式访问它们:

myScroll[0];
myScroll[1];
  // ...and so on
scrolls.myScroll1;
scrolls.myScroll2;
  // ...and so on

如果您想要
myScroll1、myScroll2
名称,则使用对象

var orderit = 0;
var scrolls = {};
$('.scrollable').each(function(){
    this.id = 'scrollp' + (++orderit);
    scrolls[ 'myScroll' + orderit ] = new iScroll( this.id );
});
…并通过以下方式访问它们:

myScroll[0];
myScroll[1];
  // ...and so on
scrolls.myScroll1;
scrolls.myScroll2;
  // ...and so on

仅供参考:不需要
.attr()
来获取和设置元素的ID。正如我在回答中所做的那样,只需使用
this.id
直接访问它



编辑:我使用了错误的值来构建对象属性。已修复。

在意识到这是我的错误之前,我在您的代码中注意到了这一点。现在实现它myScroll的每个化身都必须是一个单独的变量。这个脚本能做到这一点吗。也就是说,myScroll1、myScroll2、myScroll3等…@RobinKnight内部它们是不同的变量,只是聚合在一起作为一个数组。以
myScroll[0]、myScroll[1]、…myScroll[n]
的身份访问它们。我在意识到这是我的错误之前注意到,在您的代码中。现在实现它myScroll的每个化身都必须是一个单独的变量。这个脚本能做到这一点吗。也就是说,myScroll1、myScroll2、myScroll3等…@RobinKnight内部它们是不同的变量,只是聚合在一起作为一个数组。以
myScroll[0]、myScroll[1]、…myScroll[n]