我有一个Javascript setInterval函数可以在Chrome中运行,但在Firefox中不行。它应该在屏幕上经常写一个字符串。我知道document.write不是一个首选方法。这是代码:
function doSomething(){
document.write("1st string ");
}
setInterval(doSomething, 2000);
谢谢(JS newb)。
答案 0 :(得分:2)
在Firefox中使用document.write
时,您需要首先使用document.open
,然后才能阅读on MDN
此外,没有人再使用document.write
,如果他们这样做,那只是多余的。
如果您的目标只是将字符串写入正文,请使用以下内容:
function safeDocumentWrite(text) {
document.body.appendChild(document.createTextNode(text));
};
或者如果您想要附加HTML,请先将其打包在<div>
中:
function safeDocumentWrite(html) {
var div = document.createElement('div');
div.innerHTML = html;
document.body.appendChild(div);
}