javascript - 这段代码结果为何颜色css没有被执行?
巴扎黑
巴扎黑 2017-04-10 17:47:40
[JavaScript讨论组]
<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'>
巴扎黑
巴扎黑

全部回复(5)
PHP中文网

因为document.write会重写整个html的内容
原先是这样的

而你点击之后,页面源码变成了这样

也就是只有amo里面的内容了
你的style样式都被覆盖替换掉了

黄舟

document.write();head里的style都写掉了。。

巴扎黑

因为用了,document.writedocument.write会清除掉所有的doucment的内容,因此你写的style也会被清除掉,你用“审元素”看一下,会发现dom里只有你write的元素了。

伊谢尔伦

document.write 把原来的网页覆盖了 所以你写的style也就不存在了

巴扎黑

你直接用 document.write 把原文档都覆盖 了,你打开你的页面看看,

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号