Javascript 聚合物阵列作为单元中的一员
我试图访问数组中的变量作为聚合元素的成员。但我不断得到“无法读取未定义的属性”。这是我的代码,我在这里得到了错误。images[This.x]:Javascript 聚合物阵列作为单元中的一员,javascript,polymer,Javascript,Polymer,我试图访问数组中的变量作为聚合元素的成员。但我不断得到“无法读取未定义的属性”。这是我的代码,我在这里得到了错误。images[This.x]: <link rel="import" href="bower_components/polymer/polymer.html"> <polymer-element name="big-picture" noscript> <template> <style> #crop {
<link rel="import" href="bower_components/polymer/polymer.html">
<polymer-element name="big-picture" noscript>
<template>
<style>
#crop
{
display: block;
position: relative;
}
#mainpictop
{
position: absolute;
width: 100%;
}
#mainpicbottom
{
position: absolute;
width: 100%;
}
</style>
<div id="crop">
<img id="mainpictop" src={{pic1source}} style="opacity:{{pic1opacity}}"></img>
<img id="mainpicbottom" src={{pic2source}} style="opacity:{{pic2opacity}}"></img>
</div>
</template>
<script>
Polymer('big-picture',{
x:1,
currentTop:0,
currentBottom:1,
numpics:5,
transitionspeed:200,
images: new Array,
pic1opacity: 1,
pic2opacity:1,
pic1source: "img/main/1.jpg",
pic2source: "img/main/2.jpg",
ready: function(){
for (i=0;i<this.numpics;i++)
{
this.images[i]="img/main/".concat(i+1).concat(".jpg");
}
setInterval(function() {
changeImageSrc()
},this.transitionspeed);
}
});
function changeImageSrc() {
if(this.pic1opacity==1)
{
this.pic1opacity=0;
this.pic2opacity=1;
if (this.currentTop!=this.currentBottom)
this.pic2source = this.images[this.x];
else
{
this.x=this.x+1;
if(this.x===this.numpics)
this.x=0;
this.pic2source = this.images[this.x];
}
this.currentBottom=this.x;
}
else
{
this.pic2opacity=0;
this.pic1opacity=1;
if (this.currentTop!=this.currentBottom)
this.pic1source = this.images[this.x];
else
{
this.x=this.x+1;
if(this.x===this.numpics)
this.x=0;
this.pic1source = this.images[this.x];
}
this.currentTop=this.x;
}
this.x=this.x+1;
if(this.x===this.numpics)
this.x=0;
}
</script>
</polymer-element>
#收成
{
显示:块;
位置:相对位置;
}
#mainpictop
{
位置:绝对位置;
宽度:100%;
}
#主卧
{
位置:绝对位置;
宽度:100%;
}
聚合物(“大画面”{
x:1,
当前顶部:0,
当前底部:1,
numpics:5,
过渡速度:200,
图片:新阵列,
PIC1容量:1,
Pic2容量:1,
图片来源:“img/main/1.jpg”,
pic2source:“img/main/2.jpg”,
就绪:函数(){
对于(i=0;i这是一个范围问题。在其他函数中使用“This”时要非常小心,因为“This”的上下文将更改为它所在的函数。在您的情况下,“This”变量已设置为changeMageSrc()的上下文
一个简单的修复方法是将对“this”的引用存储在另一个变量中(var\u this=this;
)当您需要对聚合变量采取操作时,请使用该新变量。您好,我尝试通过添加对元素本身的引用来解决它,但这似乎不起作用。然后我尝试将元素本身传递给函数,但现在我遇到了与第一次相同的错误