function chumpage() {
chump = document.getElementByClassName("first-txt");
for (var i = 0; i < chump.length(); i ) {
chump[i].innerText = "temp3";
}
}
<head>
<style>
.temp {
margin: 3%;
position: relative;
}
.first-txt {
position: absolute;
top: 17px;
left: 50px;
}
.second-txt {
position: absolute;
bottom: 20px;
left: 10px;
}
</style>
</head>
<body>
<div class="temp">
<img src="">
<h3 class="first-txt">
Temp1
</h3>
<h3 class="second-txt">
Temp2
</h3>
</div>
</body>
</html>
所以我的目標是使用js將temp1的內容更改為temp3,但是上面的功能不起作用,現在不存在的影像不是問題。我將不勝感激任何指導。
uj5u.com熱心網友回復:
首先你需要呼叫方法,而不是getElementByClassName它的getElementsByClassName.
chump.length 不是函式。
function chumpage() {
const chump = document.getElementsByClassName("first-txt");
for (var i = 0; i < chump.length; i ) {
chump[i].innerText = "temp3";
}
}
chumpage();
<head>
<style>
.temp {
margin: 3%;
position: relative;
}
.first-txt {
position: absolute;
top: 17px;
left: 50px;
}
.second-txt {
position: absolute;
bottom: 20px;
left: 10px;
}
</style>
</head>
<body>
<div class="temp">
<img src="">
<h3 class="first-txt">
Temp1
</h3>
<h3 class="second-txt">
Temp2
</h3>
</div>
</body>
</html>
uj5u.com熱心網友回復:
你拼錯了
document.getElementByClassName("first-txt");
將其更改為:
document.getElementsByClassName("first-txt");
而chump.length不是chump.length()
代碼相同:
function chumpage() {
chump = document.getElementsByClassName("first-txt");
for (var i = 0; i < chump.length; i ) {
chump[i].innerText = "temp3"
}
}
chumpage()
.temp {
margin: 3%;
position: relative;
}
.first-txt {
position: absolute;
top: 17px;
left: 50px;
}
.second-txt {
position: absolute;
bottom: 20px;
left: 10px;
}
<div class="temp">
<img src="">
<h3 class="first-txt">
Temp1
</h3>
<h3 class="second-txt">
Temp2
</h3>
</div>
uj5u.com熱心網友回復:
嘗試這個:
function chumpage() {
var chump = document.getElementsByClassName('first-txt');
for (var i = 0; i < chump.length; i ) {
chump[i].innerText = "temp3";
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/486666.html
標籤:javascript html
