Bootstrap 为什么我不能得到一个html元素的值使用javascript [duplicate]

a6b3iqyw  于 2023-03-21  发布在  Bootstrap
关注(0)|答案(2)|浏览(150)

此问题在此处已有答案

How to get the pure text without HTML element using JavaScript?(10个答案)
Why does jQuery or a DOM method such as getElementById not find the element?(6个答案)
3天前关闭。
这里是html内容

<div id="result-box" class="col-5">
                <h3 class="result" id="name" >Name: --- </h3>
                <h3 class="result" id="age">Age: ---</h3>
                <h3 class="result" id="class">Class: --- </h3>
                <h3 class="result" id="skill">Skill:--- </h3>
                <h3 class="result" id="gen">Gender: --- </h3>
                <h3 class="result" id="perform">Perfomance: --- </h3>
            </div>

这是js代码你告诉我这里面有什么错误

let name = document.getElementById('name').value;
let age = document.getElementById('age').value;
let class = document.getElementById('class').value;
let skil = document.getElementById('skill').value;
let gen = document.getElementById('gen').value;
let perform = document.getElementById('perform').value;

let name = document.getElementById('name').value
console.log(name)

在控制台面板中显示未定义,这背后的原因是什么,请指导

wqnecbli

wqnecbli1#

let name = document.getElementById("name").innerText;
let age = document.getElementById("age").innerText;

let skill = document.getElementById("skill").innerText;
let gen = document.getElementById("gen").innerText;
console.log(name, age, skill, gen);
9njqaruj

9njqaruj2#

你犯了几个错误,可能会导致你的行为:

  • 你不应该定义两次“name”。如果你想覆盖变量,那么你就不能重复“let”。
  • 不能将“class”用作变量,它是保留的

相关问题