【发布时间】:2020-10-31 18:31:09
【问题描述】:
const printSentence = (id, sentence, speed = 50) => {
let index = 0;
let element = document.getElementById(id);
let timer = setInterval(function() {
const char = sentence[index];
if (char === '<') {
index = sentence.indexOf('>', index); // skip to greater-than
}
element.innerHTML = sentence.slice(0, index);
if (++index === sentence.length) {
clearInterval(timer);
}
}, speed);
} // printSentence
printSentence(
'one',
'<h2>Wordpress</h2>',
50
);
<div id="one"></div>
但我在控制台中看到一个错误:
未捕获的类型错误:无法将属性“innerHTML”设置为 null
我尝试了window.onload,但它不起作用
有关如何在控制台中解决此问题的任何提示?
【问题讨论】:
-
id是什么 - 你的 html 是什么样的 -
当您调用
printSentence('one', ...)时,没有id 为one的元素。我们应该如何解决这个问题? -
不,我只是忘了我有一个 id 为 one 的 div
-
我的评论仍然有效。当您调用
printSentence('one', ...)时,DOM 中没有具有该 ID 的元素。否则.getElementById('one')不会返回null -
你可以在我的wordpress网站上看到上面的sn-p在google chrome中的控制台显示错误,但是在stackoverflow sn-p的控制台中它没有显示任何错误
标签: javascript wordpress console