Javascript/Jquery通过带有事件的对象切换

Javascript/Jquery通过带有事件的对象切换,javascript,arrays,function,toggle,aframe,Javascript,Arrays,Function,Toggle,Aframe,我有一个带有两个箭头和一些对象的aframe场景,我尝试在这些对象之间切换,但我没有得到正确的结果。我得到了一些帮助: AFRAME.registerComponent("fool", { init: function() { var boxarray = ["#bluebox, #yellowbox, #greenbox] boxarray[0] = true; if(boxarray[0] = true){

我有一个带有两个箭头和一些对象的aframe场景,我尝试在这些对象之间切换,但我没有得到正确的结果。我得到了一些帮助:

  AFRAME.registerComponent("fool", {
    init: function() {
        var boxarray = ["#bluebox, #yellowbox, #greenbox]

        boxarray[0] = true;

        if(boxarray[0] = true){
          document.getElementById('bluebox').setAttribute('visible', 'true');
          document.getElementById('bluebox').setAttribute('scale', {x:1,y:1,z:1});
        } else {
          document.getElementById('bluebox').setAttribute('visible', 'false');
          document.getElementById('bluebox').setAttribute('scale', {x:0,y:0,z:0});
        }

        if(boxarray[1] = true){
          document.getElementById('bluebox').setAttribute('visible', 'true');
          document.getElementById('bluebox').setAttribute('scale', {x:1,y:1,z:1});
        } else {
          document.getElementById('bluebox').setAttribute('visible', 'false');
          document.getElementById('bluebox').setAttribute('scale', {x:0,y:0,z:0});
        }

        if(boxarray[2] = true){
          document.getElementById('bluebox').setAttribute('visible', 'true');
          document.getElementById('bluebox').setAttribute('scale', {x:1,y:1,z:1});
        } else {
          document.getElementById('bluebox').setAttribute('visible', 'false');
          document.getElementById('bluebox').setAttribute('scale', {x:0,y:0,z:0});
        }

      function toggleright(){
?help?
      }

      function toggleleft(){
?help?
      }
      })

我尝试给我的所有对象(框)一个事件,因此如果事件也发生了变化,则更改源非常有用。

让一个方法切换“下一个”实体可见,而“上一个”实体不可见

这样,左/右方法将只确定下一个应该是哪个,并使用切换功能

可以使用如下组件完成:

AFRAME.registerComponent("foo", {
 init: function() {
  this.objects = ["one", "two", "three"]
  this.iterator = 0
  this.left = AFRAME.utils.bind(this.left, this);
  this.right = AFRAME.utils.bind(this.right, this);
 },
 right: function() {
  let i = (this.iterator - 1) < 0 ? this.objects.length - 1 : this.iterator - 1
  this.toggle(this.iterator, i)
 },
 left: function() {
  let i = (this.iterator + 1) >= this.objects.length ? 0 : this.iterator + 1
  this.toggle(this.iterator, i)
 },
 toggle: function(oldEl, newEl) {
  document.getElementById(this.objects[oldEl]).setAttribute("visible", "false")
  document.getElementById(this.objects[newEl]).setAttribute("visible", "true")
  this.iterator = newEl
 }
})
AFRAME.registerComponent(“foo”{
init:function(){
this.objects=[“一”、“二”、“三”]
this.iterator=0
this.left=AFRAME.utils.bind(this.left,this);
this.right=AFRAME.utils.bind(this.right,this);
},
右:函数(){
设i=(this.iterator-1)<0?this.objects.length-1:this.iterator-1
this.toggle(this.iterator,i)
},
左:函数(){
设i=(this.iterator+1)>=this.objects.length?0:this.iterator+1
this.toggle(this.iterator,i)
},
切换:功能(oldEl、newEl){
document.getElementById(this.objects[oldEl]).setAttribute(“可见”、“假”)
document.getElementById(this.objects[newEl]).setAttribute(“可见”、“真实”)
this.iterator=newEl
}
})
right
left
方法仅检查是否到达数组的开头/结尾,并调用切换可见性的
方法


实时小提琴。

您需要执行
if(boxarray[x]==true)
。执行单个
=
是一项任务,需要以不同的方式执行,因为使用布尔值覆盖字符串数组(或将字符串与布尔值进行比较)可以完美地工作thx@delidagon我很高兴能提供帮助,我希望这非常清楚:)