gpt4 book ai didi

javascript - 在新创建的窗口中创建一个 div

转载 作者:行者123 更新时间:2023-12-02 16:03:45 25 4
gpt4 key购买 nike

我制作了一个计时器来显示我新创建的窗口。

但是,它每次都会重复文本,因为我写了一个新行。

我想在我的窗口中创建一个 div 元素,这样我就可以每秒替换(更新)文本。

对象:

venster = window.open('new.html','titel','width=500, height= 500');

我的计时器:

function stopwatch(sec){
//timer
venster.document.write("window closes in: " + sec);
if(sec < 1){
clearTimeout(timer);
venster.document.write("<h1>i will close!</h1>");
//setTimeout(venster.close(),2000);
}
sec--;
var timer = setTimeout('stopwatch('+sec+')',1000);
}

我希望文本“窗口在..后关闭”仅显示一次,而不是每秒显示该文本的新行。

最佳答案

您缺少 else 子句。如果您想替换整个内容,那么 innerHTML 就是您想要的。 document.write 始终只是添加。

function stopwatch(sec){
if(sec < 1){
venster.document.body.innerHTML = "<h1>i will close!</h1>"; // Also this h1 wasn't closed
setTimeout(venster.close ,1000); // And this was executed immediately rather than passing on a function
} else {
venster.document.body.innerHTML = "window closes in: " + sec;
setTimeout(function() { stopwatch(sec-1) }, 1000); // And this would look prettier wrapped in an anonymous function
}
}

关于javascript - 在新创建的窗口中创建一个 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30969578/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com