<script>
function amo()
{
document.write("<div class='x'>eee</div>")
document.write("<div class='x'>eee</div>")
document.write("<div class='x'>eee</div>")
}
</script>
<style>
.x{width:50px;height:50px;background-color:red;}
</style>
<input type=button onclick='amo()' value='fff'>
因为document.write
会重写整个html
的内容
原先是这样的
而你点击之后,页面源码变成了这样
也就是只有amo
里面的内容了
你的style
样式都被覆盖替换掉了
document.write();
把head
里的style
都写掉了。。
因为用了,document.write
,document.write
会清除掉所有的doucment的内容,因此你写的style
也会被清除掉,你用“审元素”看一下,会发现dom里只有你write的元素了。
document.write 把原来的网页覆盖了 所以你写的style也就不存在了
你直接用 document.write 把原文档都覆盖 了,你打开你的页面看看,