【问题标题】:Index variable not working, but integer does索引变量不起作用,但整数起作用
【发布时间】:2022-06-13 20:33:22
【问题描述】:

我希望你今天过得愉快。 ???因为我目前不是...有没有人知道为什么会这样:

 customizer.sections.text.previews[0].setAttribute('data-color', 'black-1800');

但这不是:

customizer.sections.text.previews[index].setAttribute('data-color', 'black-1800');

我的调试尝试:

console.log(typeof index);
console.log(index);
console.log(index === 0);
console.log(customizer.sections.text.previews[index])
number
0 
true
<span data-preview="text" data-color data-font class="outline">Hello</span>

我已经在这个问题上卡了大约一个半小时......

这是完整的上下文:

function getTextColors(index) {
  if (index !== null) {
    console.log(customizer.sections.text.previews[0])
    if (getCurrentVariantOptions()[0].toLowerCase() === 'black')
    customizer.sections.text.previews[index].setAttribute('data-color', 'white-1801')
    if (getCurrentVariantOptions()[0].toLowerCase() === 'white')
    console.log(typeof i);
    console.log(i);
    console.log(i === 0);
    console.log(customizer.sections.text.previews[i])
    customizer.sections.text.previews[i].setAttribute('data-color', 'black-1800'); 
  } else {
  if (getCurrentVariantOptions()[0].toLowerCase() === 'black')
    customizer.sections.text.previews.forEach(elem => elem.setAttribute('data-color', 'white-1801'))
  if (getCurrentVariantOptions()[0].toLowerCase() === 'white')
    customizer.sections.text.previews.forEach(elem => elem.setAttribute('data-color', 'black-1800'))
  }
}

customizer.sections.text.fontInputs.forEach((elem, index) => {
    elem.addEventListener('change', () => {
      previewTextFont(customizer.sections.text.previews, elem.value, index)
      previewTextFont(customizer.sections.text.navPreviews, elem.value, index)
      dynamicOutline(customizer.sections.text.previews[index])
      if (index == 0 || index == 1 || index == 2) {
        getTextColors(index)
      }
    })
  })

【问题讨论】:

  • 你在哪里定义i变量?

标签: javascript variables indexing integer


猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-02
  • 2017-12-04
  • 2014-03-25
相关资源
最近更新 更多