gpt4 book ai didi

javascript - document.write () 应该在我的页面加载后删除所有现有的 html 吗?

转载 作者:行者123 更新时间:2023-11-28 04:46:12 26 4
gpt4 key购买 nike

我红了这个:

The write() method is mostly used for testing: If it is used after an HTML document is fully loaded, it will delete all existing HTML.

所以我决定测试一下,我写了这段代码:

<!doctype html> 
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<p>I'm TEST which should be a deleted?</p>
<script>
var drink = "Red bull";
var lyrics = "";
var cans = 99;

while(cans > 0)
{
lyrics += cans + " cans of " + drink + " on the wall <br>";
cans--;
}
document.write(lyrics);
</script>
</body>
</html>

如你所见,我添加了 <p>I'm TEST which should be a deleted?</p>之后我调用了document.write在我的script之间应该删除包括该段落在内的所有现有 html 的标记?

但接下来是输出:

enter image description here

但是段落还在,不是应该按照这句话去掉吗:

If it is used after an HTML document is fully loaded, it will delete all existing HTML.

最佳答案

document.write(lyrics); 在页面加载期间被调用。

在函数中声明:

<script>  
function writeData(){
document.write(lyrics);
}
</script>

并在文档完全加载时调用该函数你应该会看到另一种行为。

例如点击按钮:

<button onclick="writeData()">WriteData</button>

关于javascript - document.write () 应该在我的页面加载后删除所有现有的 html 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45256932/

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