<script>
function amo()
{
document.write("<p class='x'>eee</p>")
document.write("<p class='x'>eee</p>")
document.write("<p class='x'>eee</p>")
}
</script>
<style>
.x{width:50px;height:50px;background-color:red;}
</style>
<input type=button onclick='amo()' value='fff'>
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
因为


document.write会重写整个html的内容原先是这样的
而你点击之后,页面源码变成了这样
也就是只有
amo里面的内容了你的
style样式都被覆盖替换掉了document.write();把head里的style都写掉了。。因为用了,
document.write,document.write会清除掉所有的doucment的内容,因此你写的style也会被清除掉,你用“审元素”看一下,会发现dom里只有你write的元素了。document.write 把原来的网页覆盖了 所以你写的style也就不存在了
你直接用 document.write 把原文档都覆盖 了,你打开你的页面看看,