我創建了一個函式updateCard(elem, name, role, bio),它接收一個要更新的元素ID,名字、角色和生物作為字串,并改變螢屏上的內容。 完整的HTML javscript代碼是:
<! DOCTYPE html>
<html lang="en">
<head>/span>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device width, initial-scale=1. 0" />
< meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>DOM</title>
</head>
<body>
<section id="card">
<h1>新學生</h1>/span>
<h3>/span>角色</h3>/span>
<p>Bio Text Goes Here</p>
</section>/span>
<script src="./index.js">/span>
function updateCard(elem, name, role, bio) {
const card = document.getElementById(elem) 。
const head = document.getElementsByTagName("h1") 。
head.textContent = name。
}
updateCard("card", "shantanu", "工程師", "just a normal person") 。
</script>>
</body>
</html>/span>
uj5u.com熱心網友回復:
getElementsByTagName回傳一個陣列,正如其名稱ELEMENTS。
只要做head[0].innerHTML。它將會起作用。
更多資訊在這里:
getElementsByTagName()方法以NodeList物件的形式回傳一個具有指定標簽名稱的元素的子元素的集合。
https://www.w3schools.com/jsref/met_element_getelementsbytagname.asp
uj5u.com熱心網友回復:
getElementsByTagName回傳一個陣列,你需要針對第一個陣列元素來更新其值,如下所述。類似地,你可以更新其余的值。完整的作業實體已被添加到
head[0].textContent = name;
作業代碼
function updateCard(elem, name, role, bio) {
const card = document.getElementById(elem) 。
const head = document.getElementsByTagName("h1") 。
const roleTag = document.getElementsByTagName("h3")。
const bioTag = document.getElementsByTagName("p") 。
head[0].textContent = name;
roleTag[0].textContent = role;
bioTag[0].textContent = bio;
}
updateCard("card", "shantanu", "工程師", "只是一個普通人") 。
<section id="card" >
<h1>新學生</h1>/span>
<h3>/span>角色</h3>/span>
<p>Bio Text Goes Here</p>
</section>/span>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
uj5u.com熱心網友回復:
你從getElementsByTagName得到陣列。
像這樣做。head[0].textContent = name;
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/333286.html
標籤:
