document.querySelector返回null

bird_blue03

querySelector在JS中使用来选择用JS脚本填充的html标记。但是,任何时候我尝试的div存储与一类.cardconst questionCard,我得到null

为什么我不能选择卡?

HTML:

<div class='card-container'></div>

JS:

const questionBlock = document.querySelector('.card-container');
const questionCard = document.querySelector('.card');

function build() {
    let output = [];

    ...some unimportant code here...

      output.push(
        `
          <div class='card'>
            <div class='question'>${current.question}</div>
            <div class='answers'>${answers.join('')}</div>
          </div>
        `
      );
    })

    questionBlock.innerHTML = output;
}

build();
丹尼斯

您需要在致电document.querySelector('.card') 致电build()它找不到尚不存在的HTML元素。

const questionBlock = document.querySelector('.card-container');

function build() {
    let output = [];

    ...some unimportant code here...

      output.push(
        `
          <div class='card'>
            <div class='question'>${current.question}</div>
            <div class='answers'>${answers.join('')}</div>
          </div>
        `
      );
    })

    questionBlock.innerHTML = output;
}

build();

const questionCard = document.querySelector('.card');

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

為什麼我的 document.querySelector 返回 null?

Document.querySelector返回null,直到使用DevTools检查元素为止

在测试jest document.querySelector中的vue组件期间,始终返回null

我的变量无法保存“ document.querySelector(”。content“)”,它返回null

Marionette document.querySelector('#test') 返回 null 但这样做。$('#test') 不会

Document.querySelector返回多个分支

第一次单击React Router Link时,document.querySelector()始终返回null,但之后将正确返回

TypeError document.querySelector(...)为null

document.querySelector()返回的元素不是Node的实例

document.querySelector 返回字符串而不是数字

document.querySelector("#name").style 返回空值的字典

document.getElementById()返回null

document.getElementById返回null

querySelector 方法返回 null

使用document.querySelector()

Javascript Document.getElementById返回null

离子2 document.getElementById返回null

document.getElementById 始终返回 null

所有 document.getElementById('') 返回 null

document.getelementbyid在循环中返回null

JS-document.querySelector()|| document.querySelector()给出类型错误

如何实现document.querySelector?

Angular中的document.querySelector

为什么我的 document.querySelector() 返回 [object HTMLDivElement] 而不是值?

无法在node.js上使用噩梦API返回document.querySelector

TypeError:null不是对象(评估'document.body.querySelector('。alert> .bar.passed')。innerText')

伪造者querySelector返回null

db.collection()。document()。get()永远不会返回null

为什么document.getElementById在这里返回null?