我有問題 洗掉字母后,單詞仍然存在
我有兩個 h1 我從每個 h1 中洗掉單詞,當所有字母都被洗掉時,我專門為它停止每個 h1 的間隔

let mySelector = document.querySelectorAll(".writ-text");
for (let l = 0; l < mySelector.length; l ) {
removeText = setInterval(function() {
// Cut the last letter of the word and print the word after editing
document.querySelectorAll(".writ-text")[l].textContent =
document.querySelectorAll(".writ-text")[l].textContent.substr(0,
document.querySelectorAll(".writ-text")[l].textContent.length - 1);
// Check if the entire word has been deleted
if (document.querySelectorAll(".writ-text")[l].textContent.length == 0) {
clearInterval(removeText)
}
}, 1000);
}
<h1 class="writ-text">gold</h1>
<h1 class="writ-text">golder</h1>
uj5u.com熱心網友回復:
您已經清除了唯一的setInterval命名removeText(全域范圍),這使得它適用于兩個文本。
使用塊作用域來解決let const
let mySelector = document.querySelectorAll(".writ-text");
for (let l = 0; l < mySelector.length; l ) {
let removeText = setInterval(function() {
// Cut the last letter of the word and print the word after editing
document.querySelectorAll(".writ-text")[l].textContent =
document.querySelectorAll(".writ-text")[l].textContent.substr(0,
document.querySelectorAll(".writ-text")[l].textContent.length - 1);
// Check if the entire word has been deleted
if (document.querySelectorAll(".writ-text")[l].textContent.length == 0) {
clearInterval(removeText)
}
}, 1000);
}
<h1 class="writ-text">gold</h1>
<h1 class="writ-text">golder</h1>
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/374269.html
標籤:javascript html for循环 清除间隔
上一篇:將多列與具有多個值的串列進行比較
