1 回答

TA贡献1111条经验 获得超0个赞
它返回 [object] 因为文本变量是一个对象,所以如果你想获取名称,你应该通过调用它的键来获取,如first.innerHtml = text.name(获取名称)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<style>
body {
font-family: "Lato", sans-serif;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}
</style>
<body>
<h1>Get latest invoice</h1>
<button class="get">Get</button>
<table>
<tr>
<th>Name</th>
<th>Price</th>
<th>Payment</th>
<th>Card Number</th>
</tr>
<tr>
<td class="first"></td>
<td class="second"></td>
<td class="third"></td>
<td class="fourth"></td>
</tr>
</table>
</body>
<script>
let bill = [
{
name : "Sandra",
price : "50$",
payment : "visa",
cardnumber:"1234 5678 9012 3456"
},
{
name : "Johnson",
price : "200$",
payment : "master",
cardnumber:"1234 5678 9012 3456"
},
]
let get = document.querySelector(".get");
let first = document.querySelector(".first");
let second = document.querySelector(".second");
let third = document.querySelector(".third");
let fourth= document.querySelector(".fourth");
get.addEventListener("click" , ()=> {
let text= bill[Math.floor(Math.random()* bill.length)];
console.log(text);
first.innerHTML = text.name;
second.innerHTML = text.name;
third.innerHTML = text.name;
fourth.innerHTML = text.name;
})
</script>
</html>
添加回答
举报